Setting the JVM via the command line on Windows

后端 未结 4 1867
长发绾君心
长发绾君心 2020-12-13 19:45

Is it possible to specify the JVM to use when you call \"java jar jar_name.jar\" . I have two JVM installed on my machine. I can not change JAVA_HOME as it may break code th

4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-13 19:54

    Yes - just explicitly provide the path to java.exe. For instance:

    c:\Users\Jon\Test>"c:\Program Files\java\jdk1.6.0_03\bin\java.exe" -version
    java version "1.6.0_03"
    Java(TM) SE Runtime Environment (build 1.6.0_03-b05)
    Java HotSpot(TM) Client VM (build 1.6.0_03-b05, mixed mode, sharing)
    
    c:\Users\Jon\Test>"c:\Program Files\java\jdk1.6.0_12\bin\java.exe" -version
    java version "1.6.0_12"
    Java(TM) SE Runtime Environment (build 1.6.0_12-b04)
    Java HotSpot(TM) Client VM (build 11.2-b01, mixed mode, sharing)
    

    The easiest way to do this for a running command shell is something like:

    set PATH=c:\Program Files\Java\jdk1.6.0_03\bin;%PATH%
    

    For example, here's a complete session showing my default JVM, then the change to the path, then the new one:

    c:\Users\Jon\Test>java -version
    java version "1.6.0_12"
    Java(TM) SE Runtime Environment (build 1.6.0_12-b04)
    Java HotSpot(TM) Client VM (build 11.2-b01, mixed mode, sharing)
    
    c:\Users\Jon\Test>set PATH=c:\Program Files\Java\jdk1.6.0_03\bin;%PATH%
    
    c:\Users\Jon\Test>java -version
    java version "1.6.0_03"
    Java(TM) SE Runtime Environment (build 1.6.0_03-b05)
    Java HotSpot(TM) Client VM (build 1.6.0_03-b05, mixed mode, sharing)
    

    This won't change programs which explicitly use JAVA_HOME though.

    Note that if you get the wrong directory in the path - including one that doesn't exist - you won't get any errors, it will effectively just be ignored.

提交回复
热议问题