0

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.

Knetic
  • 2,099
  • 1
  • 20
  • 34
  • Possibily related to this question: "Programmatically determine what JDK/JRE's are installed on my box" (http://stackoverflow.com/questions/1189192/programmatically-determine-what-jdk-jres-are-installed-on-my-box) and to this question "How to programatically get all Java JVM installed (Not default one) using Java?" (http://stackoverflow.com/questions/6493856/how-to-programatically-get-all-java-jvm-installed-not-default-one-using-java) – yasouser Jul 24 '11 at 18:23

3 Answers3

2

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%

AlexR
  • 114,158
  • 16
  • 130
  • 208
  • I'm going to accept this one, since i didn't even think of checking the installer registries, and that's pretty bulletproof. Thanks! – Knetic Jul 24 '11 at 19:57
1

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.

zacheusz
  • 8,750
  • 3
  • 36
  • 60
0

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

Michael-O
  • 18,123
  • 6
  • 55
  • 121
  • Afaik eclipse uses a config file that contains the path to the JDK/JRE, so not much guessing involved there. – Voo Jul 24 '11 at 21:39
  • Actually, it doesn't in the default case. You can, however, supply one. -startup plugins/org.eclipse.equinox.launcher_1.1.1.R36x_v20101122_1400.jar --launcher.library plugins/org.eclipse.equinox.launcher.win32.win32.x86_1.1.2.R36x_v20101222 -product org.eclipse.epp.package.jee.product --launcher.defaultAction openFile --launcher.XXMaxPermSize 256M -showsplash org.eclipse.platform --launcher.XXMaxPermSize 256m --launcher.defaultAction openFile -vmargs -Dosgi.requiredJavaVersion=1.5 -Xms256m -Xmx768m This is mine and no JRE supplied. – Michael-O Jul 25 '11 at 08:46