1

I'm running a little terminal jar tool by batch with:

%cd%
"c:\program files (x86)\Java\jre7\bin\java" -jar myfile.jar

But for every new machine I'm running this, I have to first check and edit the path to java home. Is there any batch way to not have to edit this all the time, but automating the java-homedir finding?

membersound
  • 81,582
  • 193
  • 585
  • 1,120
  • You could check the environment variable `JAVA_HOME` although that is not always set either. Why don't you just rely on the installer having put `java.exe` into the `PATH`? –  Oct 01 '12 at 13:43
  • because eg for the machine I'm currently working on there is NO java command available, no path. And cannot be set due to missing admin rights. – membersound Oct 01 '12 at 13:46
  • Or you can attempt to call java ( `java -jar ...` ) without the path with hope that if it was installed than it will be in the path. – Germann Arlington Oct 01 '12 at 13:47
  • You don't need admin rights to change the (user specific) `PATH` variable. –  Oct 01 '12 at 13:48
  • The JRE installer should put an executable in the system directory (which will be on your path.) The `-version:foo` switch can be used to select a specific version. See also [How can I detect the installed Sun JRE on Windows?](http://stackoverflow.com/questions/1339910) – McDowell Oct 01 '12 at 14:04

1 Answers1

1

On Windows you can check in order:

  1. JAVA_HOME environment variable.
  2. java executable in the path.
  3. reg query "HKLM\Software\JavaSoft\Java Runtime Environment\" to list all yours java runtimes.

With the last command you can see the CurrentVersion and with reg query "HKLM\Software\JavaSoft\Java Runtime Environment\your_version_number" you will find the java home dir.

If all previous methods fail you need to scan the file system to find a java binary :(.

Luca
  • 4,223
  • 1
  • 21
  • 24