[SOLVED] Execute java with Qt QProcess in Windows OS



  • Hi all, i'm a noob in Qt programming. As title say, I'm trying to execute a java downloaded executable jar with my application, on windows os.. the only way I found is using QConnet and lauch a file .bat.

    In the file myFile.bat there's:

    java -jar dwnUtility.jar -input %1 -output %2 -quality %3

    In cpp I use

    @proc->start("D:\Work\myAppDir\myFile.bat", args);@

    This way works... but if use:

    @proc->start("C:\Windows\System32\java.exe", QStringList() << "-jar dwnUtility.jar" << "-input D:\Work\Qt_prj\file1.swf" << "-output D:\Work\Qt_prj\file2.swf" << "-quality 0.5");
    @

    and show the output with

    @
    connect(proc, SIGNAL(readyReadStandardOutput()), this, SLOT(rightMessage()) );
    connect(proc, SIGNAL(readyReadStandardError()), this, SLOT(wrongMessage()) );
    @

    I obtain this error msg

    "Could not create the Java virtual machine.
    Unrecognized option: - jar dwnUtility.jar"

    Where I wrong? The .jar app compress all images present in file1 and make the file2 with the elaborate images

    I'd prefer use the proc without call the file .bat... but in this way works.
    Any idea?



  • What is the space between the - and the jar doing in your argument?



  • delete the space between the dash and jar (make "- jar" to "-jar"). And you also need to add the arguments to the switches separately:

    @
    QStringList args;
    args << "-jar" << dwnUtility.jar";
    args << "-input" << "D:\Work\Qt_prj\file1.swf";
    args << "-output" << "D:\Work\Qt_prj\file2.swf";
    args << "-quality 0.5";

    proc->start("C:\Windows\System32\java.exe", args);
    @



  • isn't in code.. a cut n' paste error while writing the post :(



  • I think you might even need to separate the quality argument into:
    @
    args << "-quality" << "0.5";
    @
    but I am not sure.



  • ok... in this way works fine

    thanks a loot :)



  • bq. I think you might even need to separate the quality argument into:
    1
    args << "-quality" << "0.5";
    but I am not sure.

    Works in both manners



  • ok, thanks. That's why I said I was not sure. Sometimes, the rules as to what goes where for argument lists for QProcess are not completely clear to me.

    Edit: I noticed this comment in the docs:
    [quote]Windows: Arguments that contain spaces are wrapped in quotes.[/quote]
    That would mean that your last argument would become wrapped in quotes if you run it on windows, and I doubt that works ok. So, for safety, I would use the two-argument form.



  • [quote author="Andre" date="1314181153"]ok, thanks. That's why I said I was not sure. Sometimes, the rules as to what goes where for argument lists for QProcess are not completely clear to me. [/quote]

    As a rule of thumb:
    Everything that you can put together into quotation marks on a shell goes into one element of the string list.

    Something like

    @
    java -jar blubb.jar
    @

    Consists of two arguments: the "-jar" "switch" and the "blubb.jar" argument to it. If you had put it into one string like this:

    @
    args << "-jar blubb.jar"
    @

    You would have a single "switch" "-jar blubb.jar" without an argument :)



  • if I use

    @args << "-jar D:\Work\Reducer\reducer.jar";@

    i have this error msg

    Could not create the Java virtual machine.
    Unrecognized option: -jar D:\Work\Reducer\reducer.jar

    but the error isn’t when use the two element separated… why?



  • Because it is treated as one single argument. Actually you need two arguments (the -jar and the list of jars).


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.