When studying Java, there are times when you want to use a different version depending on the project. For example, I like the latest java10 and later var type inference, but I can only use java8 in my business. Depending on where you reside, java4 there were. There are times when you have to study in an old environment. Rather, as of 2020, java10 can hardly be used in business ... There is a screen to specify jvm in eclipse etc., but it is an experiment how to actually specify it on the command line.
I have a Main class compiled with java12 It is assumed that the environment variable JAVAHOME is set to the path to the JVM of java12. In that state, place the JVM of the java8 JVM in any directory, and enter the directory containing the java command in it.
bash
java -version
Hit the command. Then
java version "12.0.1" 2019-04-16
Was displayed. In other words, even in the current directory, the JVM that passes through the classpath of the environment setting JAVAHOME is running.
Therefore,
bash
./java -version
I explicitly specified the java command in the current directory. Then
java version "1.8.0_144"
And the JVM of the current directory started. So if you make the java command a full path or a relative path, you can pass the path no matter where you place the JVM?
So, I tried typing the following command in the project root with the JVM placed directly under the project root
bash
./jdk1.8.0_144.jdk/Contents/Home/bin/java -cp ./bin Main
Then
Exception in thread "main" java.lang.UnsupportedClassVersionError: Main has been compiled by a more recent version of the Java Runtime
I got this error. It is said that this Main class cannot be run on java8 because it is built on java12. In other words, the switch is successful.
Recommended Posts