QProcess - starting Java app - wrong environment?



  • I have to start a Java application.

    I tried with the usual QProcess solution:

    @QProcess process;

        if(!workingDir.isEmpty()) {
            process.setWorkingDirectory(workingDir);
        }
        process.start(appPath, args, QProcess::ReadOnly);
        process.waitForFinished(50000);
    
        DLog("Std out: ") << process.readAllStandardOutput();
        DLog("Std error: ") << process.readAllStandardError();@
    

    The Java app starts, but I have a problem with it:
    if I run the app "java" with the arg "-version" it prints that the java version is "java version "1.6.0_29"" although I installed 1.6.0_31, and even restarted my Windows 7. Also if I open up a command line and type in "java -version" it prints the new 1.6.0_31 version, but not from any Qt app (I've made test apps and all prints the "1.6.0_29" version).

    How could I solve this problem?
    Thanks in advance.

    My dev machine:
    Win 7 x64, Qt 4.7.4, MSVC 2008



  • I also tested it with MinGW, Qt 4.8.4 - I've got the same issue.



  • Did you set the _29 version in PATH (enviroment variable)? maybe you have it there..

    how are you lauching the app? I mean what's in appPath?



  • Thanks for the quick reply.

    As I mentioned I can start the app on my machine, that's not a problem (to answer your question: in case I want to run a Java app the appPath is "java" and the args are: "-jar" "the-java-app-path", and it starts the app).

    I'm concerned about the wrong Java version -> I tested that with appPath = "java" and args containing only 1 arg: "-version". In this case it prints the wrong version (_29) - and if I type it directly to a command line ("java -version") it prints the correct version (_31).

    [quote author="Iktwo" date="1331149229"]Did you set the _29 version in PATH (enviroment variable)? maybe you have it there..

    how are you lauching the app? I mean what's in appPath?[/quote]



  • I understand the problem, but it seems you have installed two versions, it's weird that if you open up a command line and type in “java -version” it prints the new 1.6.0_31 version, that's why I wanted to know how are you launching your app and what's in your PATH variable, maybe you added both versions to PATH? I don't know what happens if you do that, let me try and see what I get



  • That's exactly I'm concerned about: why do I get different versions from command line and from a Qt app. I have to make it work on any system - the client app (Qt app ;) ) checks the installed Java version and installs a new version if required. But after the newer version is installed I still get the old java version from my Qt app although when I print the version from command line I get the correct, new version.

    In case no Java version is installed and my app installs the required Java version everything works fine, even without restarting the app. But if there's a previously installed Java version the app gets the wrong version, even if I restart the whole OS.

    I checked the PATH and there's no Java related item in it.

    BUT! I found 2 different Java versions in my machine! 1 in "Program Files (x86)\Java" and another one in "Program Files\Java". The (x86) contains the wrong _29 version and the "Program Files\Java" contains the _31 version.

    I also found a Java related Environment Variable: QTJAVA which points to the wrong dir (the "Program Files (x86)\Java" one). I don't even know why this entry sits in the Env. Vars. (I don't use any Qt-Java binding, the app is pure C++/Qt) but probably this one causes the problem.



  • Oh, so you have installed both x64 and x86 versions, that may be the problem, but still I don't understand why are you getting different outputs, I don't know if windows have different environment variables for x86 apps and x64, I think that because you said you get _29 from Qt, and that's the one for 32bit and the _31 it's the 64, so if there are two PATH variables Qt it's loading the x86 PATH

    look at this it may help
    http://superuser.com/questions/247614/different-path-environment-variable-for-32bit-and-64bit-windows-is-it-possible



  • I think I'm getting closer...

    If I'm right the problem is this:
    My development machine is a 64 bit Win 7, but my Qt app is 32 bit. It detects that the system is 64 bit so it will install the 64 bit Java runtime - but a 32 bit version (the _29 in the "Program Files (x86)" folder) is also installed and I think because the app is 32 bit it will get the 32 bit Java when it starts a Java app.



  • Sorry for the answer duplication, I didn't notice you've already posted your answer.

    So I guess the solution in my case will be to install the 32 bit java runtime even in a 64 bit machine and that should work.

    Thanks for your time!



  • That's probably it, I'm gonna search more info about it, hope you work it out :)



  • Thanks, in case I find a better solution I'll definitely post it here :)



  • For those who face the same problem:

    If you start a java application with QProcess it will start java with the same bitness of the Qt app!

    It means: if your Qt app is 32 bit and you run it on a 64 bit Windows it won't find the 64 bit Java, even if there's no 32 bit Java installed only the 64 bit! It will only find the 32 bit Java (if installed)!


Log in to reply
 

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