[英]When I try to run my code with the “java” command in the cmd window, I get an error
I'm trying to learn how to program in java for the first time and I'm getting this error when I try to run the code in the cmd: 我正在尝试第一次学习如何在Java中编程,当我尝试在cmd中运行代码时遇到此错误:
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 56.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)
It works in the IDE I'm using (intelliJ). 它在我正在使用的IDE(intelliJ)中工作。
Your answer is in this line: 您的答案在这一行:
Exception in thread "main" java.lang.UnsupportedClassVersionError:
Main has been compiled by a more recent version of the Java Runtime
(class file version 56.0), this version of the Java Runtime only
recognizes class file versions up to 52.0
You have a version mismatch. 您的版本不匹配。 You have compiled your code in IntelliJ using Java 12, but you are using Java 8 in your cmd (you can confirm this using
java -version
command). 您已经使用Java 12在IntelliJ中编译了代码,但是您在cmd中使用了Java 8(可以使用
java -version
命令进行确认)。
The solution is to set your cmd to Java 12 (this can be done by changing the JAVA_HOME and PATH environment variables in Windows). 解决方案是将cmd设置为Java 12(可以通过在Windows中更改JAVA_HOME和PATH环境变量来完成)。
FYI this answer explains the class file version numbers you are seeing in the error message. 仅供参考, 此答案说明您在错误消息中看到的类文件版本号。
What's happening here is that you have an older version of Java in your system than the one IntelliJ is compiling for. 这里发生的是,您的系统中的Java版本比IntelliJ编译的Java版本要旧。 It works in IntelliJ because it has its own Java executable inside.
它在IntelliJ中工作,因为它内部有自己的Java可执行文件。
If you run java -version
in the command line, it will output the version you have in the system; 如果在命令行中运行
java -version
,它将输出您在系统中拥有的版本; you can select that as the compilation target in your IDE so it will work properly when you run it form cmd. 您可以在IDE中选择该对象作为编译目标,以便在以cmd格式运行它时可以正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.