尝试通过cmd运行Java代码时出现JNI错误
编译任何Java代码后,当我尝试从cmd运行它时,都会得到以下信息:
After compiling any Java code, when I try run it from cmd I get this:
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.UnsupportedClassVersionError: HelloWorld has been compiled by a more recent version of the Java Runtime (class file version 55.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)
我有:
jdk-11.0.1
jre1.8.0_192
我的 PATH
环境中也有 jdk/bin
.当我使用IDE并且PC可以识别 java/c -version
命令时,代码可以正常运行.
I also have jdk/bin
in my PATH
enviroment virable. Code runs fine when I use IDE and my PC recognizes java/c -version
command.
class file version 55.0
means this .class file is compiled with JDK 11. This file is allowed to run on Java 11 or above, but your java
is using that jre1.8.0_192
.
您应该更改您的 JAVA_PATH
或 PATH
环境变量,以使命令 java
指向 java ,例如位于您
jdk-11.0.1
中的一个,或在您的 jdk-11.0.1
中使用该 java.exe
>运行此.class文件.
You should change your JAVA_PATH
or PATH
environment variable to make the command java
points to a Java 11 version of java
, for example that one located in your jdk-11.0.1
, or use that java.exe
inside your jdk-11.0.1
to run this .class file.