Setting the JVM via the command line on Windows

后端 未结 4 1732
长发绾君心
长发绾君心 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:52

    You should be able to do this via the command line arguments, assuming these are Sun VMs installed using the usual Windows InstallShield mechanisms with the JVM finder EXE in system32.

    Type java -help for the options. In particular, see:

    -version:<value>
                  require the specified version to run
    -jre-restrict-search | -jre-no-restrict-search
                  include/exclude user private JREs in the version search
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-13 19:59

    yes I often need to have 3 or more JVM's installed. For example, I've noticed that sometimes the JRE is slightly different to the JDK version of the JRE.

    My go to solution on Windows for a bit of 'packaging' is something like this:

    @echo off
    setlocal
    @rem  _________________________
    @rem
    @set  JAVA_HOME=b:\lang\java\jdk\v1.6\u45\x64\jre
    @rem
    @set  JAVA_EXE=%JAVA_HOME%\bin\java
    @set  VER=test
    @set  WRK=%~d0%~p0%VER%
    @rem
    @pushd %WRK%
    cd 
    @echo.
    @echo  %JAVA_EXE%  -jar %WRK%\openmrs-standalone.jar
           %JAVA_EXE%  -jar %WRK%\openmrs-standalone.jar 
    @rem
    @rem  _________________________
    popd
    endlocal
    @exit /b
    

    I think it is straightforward. The main thing is the setlocal and endlocal give your app a "personal environment" for what ever it does -- even if there's other programs to run.

    0 讨论(0)
  • 2020-12-13 20:04

    If you have 2 installations of the JVM. Place the version upfront. Linux : export PATH=/usr/lib/jvm/java-8-oracle/bin:$PATH

    This eliminates the ambiguity.

    0 讨论(0)
提交回复
热议问题