How to have two different java SDK on path on windows

Andymoo00 :

I have a program which seems to only work with a later version of java (14.1.03), however I don't want to uninstall my current up-to-date java version. I was wondering if it's possible to have 2 instances of java in the path on windows 10 and be able to compile programs through the command prompt with choice on which instance of java has compiled the program.

Thanks in advance!

Stephen C :

Basically, putting multiple Java installation directories onto %PATH% won't work.

The %PATH% variable is a list of directories for the command processor to search to resolve command names. When you tell the shell to run java, it looks at each directory in turn until it finds an executable command that matches java (e.g. java.exe, java.bat, etc). Then it stops looking.

So if you have Java 11 and Java 14 on the %PATH%, one of them will be earlier the list than the other. The Java commands will come from that Java install, and the other install will (typically) be ignored.


Solutions:

One approach is to set %PATH% according to the Java install that you want to use right now:

  • You could do this by embedding a local %PATH% setting in the batch file you use to launch your application.
  • You could do it interactively
  • You could do it by having user (or account) specific %PATH% settings.

Another approach is to use absolute command names; e.g. C:/JDK11/bin/java or C:/JDK14/bin/java instead of java. (Use appropriate paths of course.)

(On Linux, there is a nice tool called alternatives that can be used for managing and switching default commands. It does it using symlinks ...)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=20834&siteId=1