簡體   English   中英

當我嘗試在cmd窗口中使用“ java”命令運行代碼時,出現錯誤

[英]When I try to run my code with the “java” command in the cmd window, I get an error

我正在嘗試第一次學習如何在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)

它在我正在使用的IDE(intelliJ)中工作。

您的答案在這一行:

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

您的版本不匹配。 您已經使用Java 12在IntelliJ中編譯了代碼,但是您在cmd中使用了Java 8(可以使用java -version命令進行確認)。

解決方案是將cmd設置為Java 12(可以通過在Windows中更改JAVA_HOME和PATH環境變量來完成)。

僅供參考, 答案說明您在錯誤消息中看到的類文件版本號。

這里發生的是,您的系統中的Java版本比IntelliJ編譯的Java版本要舊。 它在IntelliJ中工作,因為它內部有自己的Java可執行文件。

如果在命令行中運行java -version ,它將輸出您在系統中擁有的版本; 您可以在IDE中選擇該對象作為編譯目標,以便在以cmd格式運行它時可以正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM