简体   繁体   English

Jar文件未在Java 6或更早版本上运行

[英]Jar file not running on Java 6 or older

My windows computer has netbeans, and JDK 7 and JRE 7. I made a program and the jar file does not run on a MAC with only Java 6. Installing Java 7 on MAC is a hassle. 我的Windows计算机具有netbeans,以及JDK 7和JRE7。我编写了一个程序,而jar文件不能仅在Java 6的MAC上运行。在MAC上安装Java 7麻烦。 Can someone help me run the file? 有人可以帮我运行该文件吗?

Stack Trace: 堆栈跟踪:

: Unsupported major.minor version 51.0
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)

That is expected. 这是预期的。 If you want to compile with to target 6 with JDK 7, you should use following (assuming that you do not use Java 7 features): 如果要使用JDK 7编译为目标6,则应使用以下命令(假设您不使用Java 7功能):

javac -source 1.6 -target 1.6 

If you use Maven, go for: 如果您使用Maven,请进行以下操作:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <configuration>
    <source>1.6</source>
    <target>1.6</target>
  </configuration>
</plugin>

As you said in comment, you use Netbeans. 正如您在评论中所说,您使用Netbeans。 I have no personal experience with it, but according documentation you can change value of Source/Binary Format (to JDK 6 in this case). 我没有任何亲身经历,但是根据文档,您可以更改Source / Binary Format的值(在这种情况下为JDK 6)。

You should compile your source code targeted for example at a Java 6 runtime. 您应该编译针对Java 6运行时的源代码。 Using Ant, it's achieved using the target attribute to the javac task. 使用Ant,可以使用javac任务的target属性来实现。 Using Maven, use the Maven Compiler Plugin . 使用Maven,使用Maven编译器插件

You need to set your compilation targets to JDK 6 (or 5, or whatever is the lowest version you want to support). 您需要将编译目标设置为JDK 6(或5,或您要支持的最低版本)。

In Netbeans, Go to the project properties (right-click on the project, select Properties_. Then in the dialog that appears, select "Sources" and change the "Source Level" to JDK 6. 在Netbeans中,转到项目属性(在项目上单击鼠标右键,选择Properties_。然后在出现的对话框中,选择“源”并将“源级别”更改为JDK 6。

You cannot, in general, run a Java 7 file with the Java 6 runtime. 通常,您无法在Java 6运行时中运行Java 7文件。 You will have to either install Java 7, or rebuild the jar under Java 6. 您将必须安装Java 7或在Java 6下重建jar。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM