Java在使用java -jar命令时似乎已过时。

huangapple go评论74阅读模式
英文:

Java appears to be outdated when using java -jar command

问题

我最近在我的Windows电脑上编写了一些Java程序。我一直在尝试使用 java -jar 来运行编译后的JAR文件,以便更清楚地查看错误,但当我尝试这样做时,我会得到以下错误:

错误:发生了JNI错误,请检查您的安装,然后重试
异常在线程“main”中:“main”java.lang.UnsupportedClassVersionError:com/company/app/GUI的Java运行时的更新版本(类文件版本57.0)进行了编译,这个版本的Java运行时仅能识别高达52.0的类文件版本

这显然意味着我的Java版本过时了。但是,当我查看Java更新程序时,它显示我正在运行最新版本。运行 java -version 会显示:

java version "1.8.0_261"
Java(TM) SE Runtime Environment (build 1.8.0_261-b12)
Java HotSpot(TM) Client VM (build 25.261-b12, mixed mode)

那么为什么命令提示符在使用较旧的版本,我该如何更改它呢?感谢您的帮助。

英文:

I have recently been writing some Java programs on my Windows computer. I have been trying to use java -jar to run compiled jars in order to see errors more clearly, but when I try to do this, I get the following error:

Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.UnsupportedClassVersionError: com/company/app/GUI 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

This obviously means that my java version is outdated. However, when I took a look at the Java Updater, it shows that I am running the latest version.
Running java -version shows this:

java version "1.8.0_261"
Java(TM) SE Runtime Environment (build 1.8.0_261-b12)
Java HotSpot(TM) Client VM (build 25.261-b12, mixed mode)

So why is Command Prompt using an older version, and how do I change it?
Thank you for your help.

答案1

得分: 2

这个错误明确指出你尝试在安装了JRE/JDK 8(主要版本52,位于PATH设置中并在运行 java -jar / java -version 命令时被调用)的环境中运行使用JDK 13(主要版本57)构建的 .jar 文件。

如果你的机器上安装了JDK 13,你需要检查环境变量 PATH 和/或 JAVA_HOME

C:\Users\hp1>echo %JAVA_HOME%
C:\Java\jdk-13.0.2

C:\Users\hp1>echo %PATH%
C:\Windows\system32;C:\Java\jdk-13.0.2\bin

C:\Users\hp1>java -version
openjdk version "13.0.2" 2020-01-14
OpenJDK Runtime Environment (build 13.0.2+8)
OpenJDK 64-Bit Server VM (build 13.0.2+8, mixed mode, sharing)

如果 PATH 指向了 JRE/JDK 8,你可以创建另一个变量并更新 PATH(复制非 Java 路径):

>set JAVA_13=**你的JDK 13路径**
>set PATH=C:\Windows\system32;%JAVA_13%\bin

如果你的机器上没有安装 JDK 13,为了解决这个问题,你需要重新构建 .jar 文件,使其与 JDK 8 兼容,前提是代码没有使用较新版本的特性。

英文:

This error clearly indicates that you try to run a .jar file built with JDK 13 (major version 57) on a JRE/JDK 8 (major version 52) which is provided in PATH setting and thus invoked when running java -jar / java -version commands.

If you have JDK 13 installed on your machine, you need to check environment variable PATH and/or JAVA_HOME:

C:\Users\hp1>echo %JAVA_HOME%
C:\Java\jdk-13.0.2

C:\Users\hp1>echo %PATH%
C:\Windows\system32;C:\Java\jdk-13.0.2\bin

C:\Users\hp1>java -version
openjdk version "13.0.2" 2020-01-14
OpenJDK Runtime Environment (build 13.0.2+8)
OpenJDK 64-Bit Server VM (build 13.0.2+8, mixed mode, sharing)

If PATH refers JRE/JDK 8, you may create another variable and update PATH (copy non-java paths):

>set JAVA_13=**path_to_your_jdk_13**
>set PATH=C:\Windows\system32;%JAVA_13%\bin

If you do not have JDK 13 on your machine, to resolve this issue you should rebuild the .jar file to make it compatible with JDK 8 providing that the code is not using any features from the newer versions.

huangapple
  • 本文由 发表于 2020年8月24日 05:14:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/63552066.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定