Recently at work I had a bizarre problem to deal with. Essentially, we are distributing an application that uses JRuby, and we were encountering a problem whenever people would use our application in Windows if they installed Java in a specific way.
A lot of Windows users install Java not by putting the bits on disk and setting JAVA_HOME, but by adding java.exe to their path. When done this way, Java works in the sense that you can type “java” in a command prompt and have it understand what that means. You can compile java from command line and run it with no problem.
The problem we experienced was that JRuby, in windows, specifically looks to see if JAVA_HOME is set. If it is not set, the JRuby executable bails. I submitted a patch to JRuby to fix this behavior, but we could not rely on that solution until it was integrated.
What we are doing now is, in the batch file that we have that calls our application, we look to see if JAVA_HOME is set and, if it is not, step through the PATH looking for java.exe. We then set JAVA_HOME based on the location. As a result, by the time the JRuby script runs, JAVA_HOME has been set.
This is actually a handy trick to look for anything in the path in a Windows batch file.
Here’s the code:
if not "%JAVA_HOME%" == "" goto javaHomeAlreadySet for %%P in (%PATH%) do if exist %%P\java.exe set JAVA_HOME=%%P..\ :javaHomeAlreadySet
Absolutely No Machete Juggling is a blog about software, programming, computers, and me. I'm a programmer working in Colorado, mostly with Java and Ruby. 
Hi dbt, in your JAVA_HOME value, you must also include the “bin” folder. java.exe exists within the “bin” folder
“…but by adding java.exe to their path.” That’s actually done by the Java installer (although you could do it manually). The Java installer puts dummy java.exe and javaw.exe files in the “\WINDOWS\system32\” folder. The dummy files use the Windows Registry data to locate the real Java (ex: “\Program Files\Java\jre6\”).
If you need to find the JDK, you can use something like:
for /d %%i in (“\Program Files\Java\jdk*”) do set JAVA_HOME=%%i
Of course, that assumes the JDK is in the default location.
I am seting my JAVA HOME through a batch file using SET JAVA_HOME=location, but it is not working. Any idea? I don’t see any other command to set JAVA_HOME through a batch file. My code is as follows:
SET JAVA_HOME=”C:\jdk1.6.0_02″