简体   繁体   English

Java-使用另一个jvm运行类的命令行

[英]Java - Command line to run a class with another jvm

I've java 1.8.0_131 by default on my machine but also java 9, so can I run a compiled class with jdk-9? 我的机器上默认安装了Java 1.8.0_131,但我的机器上默认安装了Java 9,因此我可以使用jdk-9运行编译的类吗? is it possible anything like this: 是否可能是这样的:

    java -v path/to/jdk-9 myClass 

Thanks 谢谢

The java command actually invokes the JVM. java命令实际上会调用JVM。 That JVM starts up, and reads your classes, and runs what you tell it to run. 该JVM将启动,并读取您的类,然后运行您告诉它运行的内容。

There is no way to tell the "starting" JVM to actually use a different JVM. 无法告诉“启动” JVM实际使用其他JVM。 java isn't some sort of wrapper that "later" invokes some binary. java不是某种“后来”调用某种二进制文件的包装器。 It is already the binary that starts the JVM. 它已经是启动JVM的二进制文件。

In other words: what you are asking for isn't possible. 换句话说:您要的是不可能的。 A simple workaround can be to define "aliases" on the command line, like java-8 to start your java8 installation, and java-9 to point to that other installation. 一个简单的解决方法是在命令行上定义“别名”,例如java-8来启动java8安装,而java-9则指向该其他安装。

By default java command call the installed java version which can be checked by command: 默认情况下,java命令调用已安装的Java版本,可以通过以下命令检查该版本:

One way could be update the java: 一种方法是更新java:

sudo update-alternatives --config java.  

O/P of above command: 以上命令的O / P:

  Selection    Path                                     Priority   Status
------------------------------------------------------------
 0            /usr/lib/jvm/java-8-oracle/jre/bin/java   1081      auto mode
* 1            /usr/lib/jvm/java-8-oracle/jre/bin/java   1081      manual mode


 Press <enter> to keep the current choice[*], or type selection number: 

In your case java 9 will also come. 在您的情况下,Java 9也将出现。 update java version and run java command. 更新Java版本并运行Java命令。

Or you can do directly call your java9 script which you can find ${Java9 Installation Directory}/jre/bin/java -v class. 或者,您可以直接调用java9脚本,该脚本可以找到$ {Java9安装目录} / jre / bin / java -v类。

PS: Above description of commands is based on ubuntu. PS:以上命令说明基于ubuntu。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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