Search code examples
javawindowsbatch-fileenvironment-variablesadoptopenjdk

Script changing JAVA_HOME environment variable not working


I recently bumped into the following problem: I have a .jar file witch is not yet ported to Java builds higher than Java 8, but prefer to use Java 13 otherwise. My environment is Windows, so I logically tried to write a script to automate changing the Path variable:

set JAVA_HOME=C:\Program Files\AdoptOpenJDK\jdk-8.0.242.08-hotspot
set Path=%JAVA_HOME%\bin;%Path%
echo Java 8 is set to path

I manually set my JAVA_HOME environment variable to C:\Program Files\AdoptOpenJDK\jdk-13.0.2.8-hotspot, and in Path I added %JAVA_HOME%\bin. Then, I added to Path the folder, which in I store the scripts for changing the Path variable to the desired version and wrote the batch files for both Java 8 and Java 13. I tested in cmd if JAVA_HOME works well:

echo %JAVA_HOME%
C:\Program Files\AdoptOpenJDK\jdk-8.0.242.08-hotspot

and it did. Also, I could successfully call my scripts (my batch file's name is switch_to_java8:

C:\WINDOWS\system32>switch_to_java8

C:\WINDOWS\system32>set JAVA_HOME=C:\Program Files\AdoptOpenJDK\jdk-8.0.242.08-hotspot

C:\WINDOWS\system32>set Path=C:\Program Files\AdoptOpenJDK\jdk-8.0.242.08-hotspot\bin;C:\Program Files (x86)\Python38-32\Scripts\;C:\Program Files (x86)\Python38-32\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\Program Files\Microsoft VS Code\bin;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\dotnet\;C:\Program Files\Microsoft SQL Server\130\Tools\Binn\;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\Tools\Binn\;C:\Program Files\Common Files\Autodesk Shared\;C:\Program Files\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files\AdoptOpenJDK\jdk-13.0.2.8-hotspot\bin;C:\Program Files\AdoptOpenJDK\scripts;C:\Users\minef\AppData\Local\Microsoft\WindowsApps

C:\WINDOWS\system32>echo Java 8 is set to path
Java 8 is set to path

It seems like it did not modify JAVA_HOME afterall and I do not quite understand why. What is the reason for that and how could it be solved? I used elevated acess during each step. Also, is it possible to write a batch file to call C:\Program Files\AdoptOpenJDK\jdk-8.0.242.08-hotspot\bin\java.exe directly? If yes, how can I do it?


Solution

  • I don't quite get what's the issue, you've set JAVA_HOME to be C:\Program Files\AdoptOpenJDK\jdk-8.0.242.08-hotspot and it did just that. Am I missing something?

    To your other question, to run C:\Program Files\AdoptOpenJDK\jdk-8.0.242.08-hotspot\java.exe, simply call %JAVA_HOME%\java.exe.