简体   繁体   中英

Finding a JRE from C++ (windows)

I'm making a small launcher program for my Java program, the idea being that i can set memory requirements, associated libraries, and stuff like that before calling Java.

However, i'm increasingly finding that there doesn't seem to be a set-in-stone way to call Java from ANYTHING.

  1. It is no longer the case that there's a java.exe in system32, so that's not guaranteed.
  2. On most computers there's a registry entry either under HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER that defines a JRE subkey, then a JavaHome value. That works on most computers, but on my new computer (fresh java install, built less than two weeks ago) these registries don't exist. There's a JRE entry in CURRENT_USER, but no JavaHome.
  3. The JAVA_HOME environment variable isn't always set, or set correctly. Specifically on machines that don't have the JDK installed.
  4. I obviously can't rely on Java being installed to C:/Program Files/Java.

How the bleep am i supposed to be able to call a java program reliably from a C++ one? Or for that matter, from anywhere? I could have my program try all four possible methods, but what if they come up empty?

Thanks in advance.

First, finding the JRE location, setting memory requirements and stuff like that is highly recommended to implement using one of available scripting languages. Due to the language of BAT files is relatively poor you can use VBS or JScript and run them using utility named cscript .

Concerning to the fact where JRE is. Typically JRE can be found in registry. For example on my machine I can see the following entry. HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Runtime Environment\\1.6.0_26

Probably you have newer installation... Anyway check entry for Java installation here: HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall In my case I have there entry {26A24AE4-039D-4CA4-87B4-2F83216012FF} where key named InstallLocation contains path to Java Home.

The next way is to check Java home environment variable.

The last way is to perform search in %ProgramFiles%

In your installer try all methods, maybe you find more than one JRE, maybe none. Then present detected JREs and allow selecting which user want to use. User can also point JRE, especially when installer did not find it.

Eclipse uses a C shim (eclipse.exe) to spawn a JVM. You might to take a look at that source code and adapt.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM