-1

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?

  • The `set` command only affects the current shell. But Java doesn't use the JAVA_HOME variable, only the PATH variable. – user207421 Mar 14 '20 at 11:32
  • @user207421 Yes, it is clear that Java uses PATH variable. I am trying to achieve just that, by using the `.%JAVA_HOME%\bin` as PATH variable and modifying the JAVA_HOME variable to the given JDK folder, so if I want to use JDK 13, the script changes my JAVA_HOME to JDK 13's folder, thus the PATH variable becoming the bin folder of the newly changed JAVA_HOME. However, as you see if watched closely the PATH variable itself did not change to `%JAVA_HOME%\bin` . – Balázs Börcsök Mar 14 '20 at 11:59
  • 1
    Your question is unclear, it would assist us in helping you if you were to [edit your question](https://stackoverflow.com/posts/60681935/edit) to better explain to us what you want. My best guess is that you want to remove `C:\Program Files\AdoptOpenJDK\jdk-13.0.2.8-hotspot\bin` from the existing `%PATH%` value and instead of adding the expanded value of `%JAVA_HOME%\bin`, you want to add it on its unexpanded form. The idea being that a simple change of `%JAVA_HOME%` would always be reflected in `%PATH%`, without the need to update both variables. Is my analysis correct? – Compo Mar 14 '20 at 18:18
  • Compo, Yes you analysis is very much right. I am sorry if this was a badly formed question. – Balázs Börcsök Mar 14 '20 at 23:38

1 Answers1

-1

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.

Eldar B.
  • 1,097
  • 9
  • 22
  • No, I clearly stated that it *did not change to that*, it can be seen when running the script and PATH variables are being listed. – Balázs Börcsök Mar 14 '20 at 12:01
  • Regarding to my other question do you think it is viable if I use a relative path instead of writing `%JAVA_HOME%` ? – Balázs Börcsök Mar 14 '20 at 12:02
  • @BalázsBörcsök it seems you've wrote your question incorrectly or have attached the wrong code, you're calling `SET JAVA_HOME=C:\Program Files\AdoptOpenJDK\jdk-8.0.242.08-hotspot` in your script, so even if you set it manually beforehand, it changes it in the script. The `%JAVA_HOME%` is a relative path already, so I don't see the reason to use a different relative path. Either way, you could use `%%~d` if you really want to. – Eldar B. Mar 15 '20 at 13:48