简体   繁体   中英

correct way to use specific jvm to run java code on machine that has multiple jvms

I have a situation where I have multiple jvms on a pc. Now I want to run java code using a specific jvm, how do I do that? I did do

set JAVA_HOME=C:\special\jre\bin\jvm.dll

but it doesn't seem to work as before or after when I do java -version, I always get the same, currently installed java eg

    Microsoft Windows [Version 10.0.17763.1098]
    (c) 2018 Microsoft Corporation. All rights reserved.

    C:\Windows\System32>java -version
    java version "1.8.0_241"
    Java(TM) SE Runtime Environment (build 1.8.0_241-b07)
    Java HotSpot(TM) Client VM (build 25.241-b07, mixed mode, sharing)

    C:\Windows\System32>

where as I am hoping that once I do the set java_home, it should work with that specific jvm? I know I am making some trivial mistake here, can some one please guide? Thanks in advance

If you run as,

C:\special\jre\bin java -version //desired vesrion

You need to set a PATH variable,

JAVA_SPECIAL_HOME = C:\special\jre\

And run as,

%JAVA_SPECIAL_HOME%\bin\java

PATH is a special variable that points on a folder where windows can find the executable file (not necessarily java, but java in this case). So if you have, say, java 10 in the path, open up cmd and type java -version - you'll see java 10. If you have many folders with "java.exe" - the one that appears first will be launched.

JAVA_HOME , on the contrary, has nothing to do with Windows - its a "convention" - many other programs (like IDEs) can assume that if you have an environment variable called JAVA_HOME - it should point on JDK installation, that's it.

So if you have multiple JVMs installed on the same computer - you'll have to point explicitly on bin/java.exe of jdk/jre of your choice. This can be done by using JAVA_HOME or without.

On my personal windows system (and I use many Java installation) I usually define the following (I assume all jdk-s are installed in folder "Java", it can be Program Files or whatever):


JAVA_8_HOME=C:\Java\jdk8

JAVA_9_HOME=C:\Java\jdk9

...

JAVA_HOME=%JAVA_14_HOME%

PATH=... %JAVA_HOME%\bin

By default I would like to use java 14 for example, but I will be able to switch the default in future. For this, I'll edit the JAVA_HOME definition and point it to another JAVA


Now I want to run something with a default java (which should be ok in the most cases) - I don't think about this at all - just run java -jar Myjar.jar

If I want some specific java (for example java 8) - I can do %JAVA_8_HOME%\bin\java -jar Myjar.jar

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM