How to launch 64 bit version of javaw.exe from CreateProcess (C++)

Santosh Tiwari picture Santosh Tiwari · Apr 26, 2011 · Viewed 18.1k times · Source

I am writing a launcher in C++ to launch my java based GUI application on Windows. I am using CreateProcess to launch "javaw.exe". Everything works except for the fact that 32-bit version of "javaw.exe" is always launched.

When java is installed, it puts the executables "java.exe" and "javaw.exe" in %windir%\System32 on 32-bit windows. On 64-bit windows, it puts the same executables in %windir%\SysWow64.

There are 3 possibilities:

  1. 32-bit launcher executed on 32-bit windows: %windir%\System32 is in search path, and 32-bit javaw.exe is found. The GUI launches. Everything works.

  2. 32-bit launcher executed on 64-bit windows: %windir%\System32 is in search path. %windir%\System32 is redirected to %windir%\SysWow64 (since my launcher is 32 bit in this case). 32-bit javaw.exe is found. The GUI launches. Everything works.

  3. 64-bit launcher executed on 64-bit windows: %windir%\System32 is in search path. No redirection happens. It does not contain the executable javaw.exe. The launcher fails.

How do I launch 64-bit javaw.exe in the third case?

Answer

Santosh Tiwari picture Santosh Tiwari · Apr 28, 2011

I finally found a solution (by digging through various posts on stackoverflow).

Recent versions of JRE when installed do put a copy of "javaw.exe" in System32. The previous versions of 64 bit JRE probably didn't (not sure).

In any case, the registry key HKEY_LOCAL_MACHINE/SOFTWARE/JavaSoft/Java Runtime Environment has a property CurrentVersion which points to the key for the default JRE for the system. The sub-key corresponding to the version number has a property JavaHome which points to the location of JRE installation.

If JRE/JDK is not installed, then the Java Runtime Environment key won't be found.