java - Java - 多次重新安装JDK后出现JNL错误

78 1

嗨,我是Java的新手,但是我试图开始使用它,但是当我尝试运行hello world脚本时,便遇到了此错误。所以我重新安装了Java ,这是我从java版本中得到的:


java version"1.8.0_231"


Java(TM) SE Runtime Environment (build 1.8.0_231-b11)


Java HotSpot(TM) Client VM (build 25.231-b11, mixed mode)



之后,我删除了以前编译过的文件,然后重试。似乎编译文件没有问题,但是当我尝试运行它时,我仍然得到


Error: A JNI error has occurred, please check your installation and try again


Exception in thread"main" java.lang.UnsupportedClassVersionError: MyClass has been compiled by a more recent version of the Java Runtime (class file version 57.0), this version of the Java Runtime only recognizes class file versions up to 52.0


 at java.lang.ClassLoader.defineClass1(Native Method)


 at java.lang.ClassLoader.defineClass(Unknown Source)


 at java.security.SecureClassLoader.defineClass(Unknown Source)


 at java.net.URLClassLoader.defineClass(Unknown Source)


 at java.net.URLClassLoader.access$100(Unknown Source)


 at java.net.URLClassLoader$1.run(Unknown Source)


 at java.net.URLClassLoader$1.run(Unknown Source)


 at java.security.AccessController.doPrivileged(Native Method)


 at java.net.URLClassLoader.findClass(Unknown Source)


 at java.lang.ClassLoader.loadClass(Unknown Source)


 at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)


 at java.lang.ClassLoader.loadClass(Unknown Source)


 at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)




public class MyClass {


 public static void main(String args[]) {


 System.out.println("Hello World");


 }


}



时间: 原作者:

95 0

问题是,你的Java运行时是版本8,而编译器是版本13,因此不兼容,如果你运行javac -version 它会告诉你类似javac 13.0.1的东西,你应该检查是否卸载了Java 8,并且仅使用JDK附带的运行时。版本不兼容应该消失。

或者你可以将--release 8添加到编译器调用中,这将告诉Java 13编译器产生与Java 8兼容的字节码。


public class Main{


 public static void main(String[] args) {


 System.out.println("Hello world!");


 System.out.println("Running on Java version" 


 + System.getProperty("java.version"));


 }


}



然后在没有附加标志的情况下编译它:


> jdk13/bin/javac .Main.java



并在Java 13上运行它:


> jdk13/bin/java Main


Hello world!


Running on Java version 13.0.1



在Java 8上:


> jdk8/bin/java Main


Error: A JNI error has occurred, please check your installation and try again


Exception in thread"main" java.lang.UnsupportedClassVersionError: Main has been compiled by a more


recent version of the Java Runtime (class file version 57.0), this version of the Java Runtime only


recognizes class file versions up to 52.0


...



按预期崩溃。

现在使用release标志编译它:


> jdk13/bin/javac --release 8 .Main.java



再次测试:


> jdk8/bin/java Main


Hello world!


Running on Java version 1.8.0_192



> jdk13/bin/java Main


Hello world!


Running on Java version 13.0.1



一切都正常。

原作者:
...