0

What would be the best way to get the location of java.home in my C++ app? Right now I'm getting it from the registry but am wondering if there's a better/easier/cleaner way.

Thanks, Serge

sjespers
  • 23
  • 4
  • The registry is probably your best bet on Windows. Out of curiosity, why do you need the location of the Java home directory? – Tom Jul 02 '10 at 17:28
  • @Tom: It's not that easy to explain but I'll try. I have this AIR 2 app that launches a JAR file. In order to launch that file, I need java.exe and since that can be installed in various locations I want to check where it is first. – sjespers Jul 02 '10 at 17:36
  • As far as I know, the Registry is the only other way... other than JAVA_HOME that is. – Powerlord Jul 02 '10 at 18:03

3 Answers3

1

if JAVA_HOME is set to the java home directory. If not val will be null.

char* val = getenv("JAVA_HOME");

Robby Pond
  • 73,164
  • 16
  • 126
  • 119
  • Setting the JAVA_HOME environment variable doesn't seem to be part of the default Java installation process and thus it won't exist in many cases. – sjespers Jul 02 '10 at 17:37
  • That's wrong. JAVA_HOME and java.home are very different beasts. See the code for the JVM. – ingyhere May 14 '13 at 03:57
0

If you're on a Unix/Linux box, perhaps you could do "which java". If you need assistance doing this in C++, see How to execute a command and get output of command within C++ using POSIX?

If you're on a Windows box, there is a "which.bat" someone made that I've used with success in the past, although it's not as good. You'd have to google for it.

Hopefully this can point you in the right direction!

Community
  • 1
  • 1
corsiKa
  • 81,495
  • 25
  • 153
  • 204
0

There is often a copy of javaw in the System32 directory of a Windows installation, so getenv("windir"), and concatenate "\System32\javaw.exe".

The second choice is then to open a File Browser dialog to choose the location of javaw.exe.

maxwellb
  • 13,366
  • 2
  • 25
  • 35