2017年2月9日 星期四

Java UnsupportedClassVersionError



UnsupportedClassVersionError happens when compiles in Java 1.8 and runs in Java 1.7
[quantprod@hk023 gen-java]$ java -cp ".:./lib/*" JavaClient
Exception in thread "main" java.lang.UnsupportedClassVersionError: JavaClient : Unsupported major.minor version 52.0
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
        at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
        at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
[quantprod@hk023 gen-java]$ java -version
java version "1.7.0_71"
OpenJDK Runtime Environment (rhel-2.5.3.2.el6_6-x86_64 u71-b14)
OpenJDK 64-Bit Server VM (build 24.65-b04, mixed mode)

Solution to UnsupprtedClassVersionError
Specify source and target to 1.7 when compiling in Java 1.8
[ricky@pc009 gen-java]$ javac -source 1.7 -target 1.7 -cp ".:./lib/*" JavaClient.java
warning: [options] bootstrap class path not set in conjunction with -source 1.7
1 warning

[ricky@pc009 bin]$ echo $JAVA_HOME
/usr/java/jdk1.8.0_101/bin
[ricky@pc009 bin]$ pwd
/usr/java/jdk1.7.0_71/bin
[ricky@pc009 bin]$ java -version
openjdk version "1.8.0_101"
OpenJDK Runtime Environment (build 1.8.0_101-b13)
OpenJDK 64-Bit Server VM (build 25.101-b13, mixed mode)
[ricky@pc009 bin]$ ./java -version
java version "1.7.0_71"
Java(TM) SE Runtime Environment (build 1.7.0_71-b14)
Java HotSpot(TM) 64-Bit Server VM (build 24.71-b01, mixed mode)


[ricky@pc009 gen-java]$ javac -source 1.7 -target 1.7 -bootclasspath /usr/java/jdk1.7.0_71/jre/lib/rt.jar -cp ".:./lib/*" JavaClient.java

/usr/java/jdk1.7.0_71/jre/lib/rt.jar

沒有留言:

張貼留言

2023 Promox on Morefine N6000 16GB 512GB

2023 Promox on Morefine N6000 16GB 512GB Software Etcher 100MB (not but can be rufus-4.3.exe 1.4MB) Proxmox VE 7.4 ISO Installer (1st ISO re...