打开 .jar 时出现“Java 异常”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25162311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 23:45:11  来源:igfitidea点击:

"A java exception has occurred" when opening .jar

javajarclient

提问by Ramin Amiri

I've made a client for my game and I wanted to jar it, I used jarmaker and did I'm sure I did everything correctly, but when I try to open the .jar file, it says "A java exception has occurred" any help is appreciated.

我为我的游戏制作了一个客户端,我想对其进行 jar,我使用了 jarmaker 并且我确信我做的一切都是正确的,但是当我尝试打开 .jar 文件时,它说“发生了 java 异常”任何帮助表示赞赏。

Exception in thread "main" java.lang.UnsupportedClassVersionError: client : Unsupported major.minor version 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.access0(Unknown Source)
    at java.net.URLClassLoader.run(Unknown Source)
    at java.net.URLClassLoader.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)

Press any key to continue . . .

按任意键继续 。. .

回答by StephaneM

You compiled your jar with a JDK version superior to the JDK used to launch the jar file (you might have compiled with 1.7 and try to run it with 1.6).

您编译 jar 时使用的 JDK 版本高于用于启动 jar 文件的 JDK(您可能使用 1.7 编译并尝试使用 1.6 运行它)。

2 solutions:

2个解决方案:

  • If you don't use specific features of the JDK you use to compile, set its target to the previous version (in the above example add the flag -target 1.6to you compiler options)
  • Upgrade the JDK used on the client.
  • 如果您不使用用于编译的 JDK 的特定功能,请将其目标设置为以前的版本(在上面的示例中,将标志添加-target 1.6到您的编译器选项中)
  • 升级客户端使用的JDK。

回答by Joshi

Upgrade the jdk version. You can upgrade it by uninstalling the older version from control panel and installing newer version of jdk. Get the new version here http://www.oracle.com/technetwork/java/javase/downloads/index.html

升级jdk版本。您可以通过从控制面板卸载旧版本并安装新版本的 jdk 来升级它。在此处获取新版本http://www.oracle.com/technetwork/java/javase/downloads/index.html

回答by Omore

I generate JAR file in NetBeansonce I double click on JAR file I got an error

我在NetBeans双击JAR 文件后生成 JAR 文件,但出现错误

A Java Exception has occurred.

发生Java异常。

I just create new folder put JAR file inside that folder and also place lib folder that is used for Java project and finally JAR file is working fine.

我只是创建新文件夹,将 JAR 文件放在该文件夹中,并放置用于 Java 项目的 lib 文件夹,最后 JAR 文件工作正常。

回答by C T Mithun

This error usually happens when compiled in higher JDK version and ran in the lower version of JDK and if your code has some features present in the higher versions. Usually this happens because by default in the OS if java -versionis searched it might show some old JDK version, so either change global env variable's value if you have privileges to do so else e.g., in linux instead of typing only 'java' while executing, type the complete path e.g., /usr/jav/jdk1.8/bin/java <<.class||.jar>>. This will resolve the issue.

当在较高版本的 JDK 中编译并在较低版本的 JDK 中运行并且您的代码具有较高版本中存在的某些功能时,通常会发生此错误。通常会发生这种情况,因为默认情况下,如果在操作系统java -version中进行搜索,它可能会显示一些旧的 JDK 版本,因此,如果您有权限,请更改全局 env 变量的值,否则,例如,在 linux 中,而不是在执行时只键入“java”,键入完整的路径,例如,/usr/jav/jdk1.8/bin/java <<.class||.jar>>。这将解决问题。