Java 在 Windows 上通过命令行设置 JVM
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/674903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting the JVM via the command line on Windows
提问by
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 that is all ready running.
是否可以在调用 "java jar jar_name.jar" 时指定要使用的 JVM。我的机器上安装了两个 JVM。我无法更改 JAVA_HOME,因为它可能会破坏已准备就绪的代码。
Kind Regards
亲切的问候
Stephen
斯蒂芬
采纳答案by Jon Skeet
Yes - just explicitly provide the path to java.exe. For instance:
是的 - 只需明确提供 java.exe 的路径。例如:
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:
对正在运行的命令 shell 执行此操作的最简单方法是:
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:
例如,这是一个完整的会话,显示了我的默认 JVM,然后是对路径的更改,然后是新的:
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.
这不会改变明确使用 JAVA_HOME 的程序。
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.
请注意,如果您在路径中得到错误的目录 - 包括一个不存在的目录 - 您不会收到任何错误,它实际上会被忽略。
回答by McDowell
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.
您应该能够通过命令行参数执行此操作,假设这些是使用通常的 Windows InstallShield 机制和system32 中的 JVM finder EXE 安装的 Sun VM 。
Type java -helpfor the options. In particular, see:
为选项键入java -help。具体见:
-version:<value>
require the specified version to run
-jre-restrict-search | -jre-no-restrict-search
include/exclude user private JREs in the version search
回答by will
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.
是的,我经常需要安装 3 个或更多 JVM。例如,我注意到有时 JRE 与 JRE 的 JDK 版本略有不同。
My go to solution on Windows for a bit of 'packaging' is something like this:
我在 Windows 上进行的一些“打包”解决方案是这样的:
@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.
我认为这很简单。主要的是 setlocal 和 endlocal 为您的应用程序提供了一个“个人环境”,无论它做什么——即使还有其他程序要运行。
回答by Loshen Naicker
If you have 2 installations of the JVM. Place the version upfront. Linux : export PATH=/usr/lib/jvm/java-8-oracle/bin:$PATH
如果您安装了 2 个 JVM。将版本放在前面。Linux:导出路径=/usr/lib/jvm/java-8-oracle/bin:$PATH
This eliminates the ambiguity.
这消除了歧义。