Question: When I try to run a Java program in Linux, I encountered an error. It looks like the program was compiled with a different version of Java with my local. How do I switch the default version of Java on Linux?
When the Java program is compiled, the compiler environment will set a "target" variable to set the minimum Java program can run version. If the program runs on a Linux system, you can not meet the minimum JRE version requirements, you will encounter the following error at run time.
Exceptionin thread "main" java.lang.UnsupportedClassVersionError: com / xmodulo / hmon / gui / NetConf: Unsupported major.minor version 51.0
For example, a program in Java JRE 1.7 compiler, but the system only Java JRE 1.6.
To solve this problem, you need to change the default version of Java to Java JRE 1.7 or higher (assuming JRE already installed).
First, try the following update-alternatives command to check your Java version available on your system:
$ Sudo update-alternatives --display java
In this example, it installed a total of four different versions of Java: OpenJDK JRE 1.6, Oracle Java JRE 1.6, OpenJDK JRE 1.7 and Oracle Java JRE 1.7. Now that the default Java version is OpenJDK JRE 1.6.
If you do not need Java JRE, you can refer to these instructions to complete the installation.
Release candidate now available, you can use the following commands to switch between the available Java JRE default Java version:
$ Sudo update-alternatives --config java
When prompted, choose what you want to try the Java version. In this example, we chose Oracle Java JRE 1.7.
Now use the following command to verify the default Java version.
$ Java -version
Finally, if you define the JAVA_HOME environment variable, update the variable settings according to your version of Java.