简体   繁体   English

当我尝试在cmd窗口中使用“ java”命令运行代码时,出现错误

[英]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.

相关问题 我的 java 编译器在命令提示符下处理我的程序,但是当我尝试运行该程序时,它给了我一个错误 - My java compiler works on my program in the command prompt but when i try to run the program it gives me an error 当我尝试运行eclipse时,我得到java代码13错误 - I get java code 13 error when i try to run eclipse 我尝试运行该类时出现Java安全性错误 - I get a Java Security Error when I try to run the class 尝试在cmd中运行我的项目时出错 - Error when try to run my project in cmd 我如何使用 java 代码以编程方式在 android 上运行 cmd 命令 - how do i run cmd command on android programmatically with java code 当我尝试在 Java 中运行我的套接字程序时,为什么会出现错误? - Why am I getting an error when I try to run my socket program in Java? 当我尝试编译我的 Java 代码时,为什么会得到“异常;必须被捕获或声明被抛出”? - Why do I get "Exception; must be caught or declared to be thrown" when I try to compile my Java code? 当我尝试运行我的代码时,我得到“ java.lang.Exception:java.lang.IllegalStateException” - I am getting “java.lang.Exception: java.lang.IllegalStateException” when i try to run my code 我有 curl 命令它在 cmd 中工作正常,但我想在 java 代码中运行它 - i have curl command its work fine in cmd but i want to run it in java code 当我尝试运行javafx应用程序时在初始化中收到错误 - getting an error in the initialize when I try to run my javafx application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM