Jacopo Sabatini
Jacopo Sabatini

Reputation: 76

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? is it possible anything like this:

    java -v path/to/jdk-9 myClass 

Thanks

Upvotes: 0

Views: 882

Answers (2)

saurav omar
saurav omar

Reputation: 76

By default java command call the installed java version which can be checked by command:

One way could be update the java:

sudo update-alternatives --config java.  

O/P of above command:

  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. update java version and run java command.

Or you can do directly call your java9 script which you can find ${Java9 Installation Directory}/jre/bin/java -v class.

PS: Above description of commands is based on ubuntu.

Upvotes: 0

GhostCat
GhostCat

Reputation: 140427

The java command actually invokes the JVM. That JVM starts up, and reads your classes, and runs what you tell it to run.

There is no way to tell the "starting" JVM to actually use a different JVM. java isn't some sort of wrapper that "later" invokes some binary. It is already the binary that starts the 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.

Upvotes: 1

Related Questions