简体   繁体   中英

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. 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):

javac -source 1.6 -target 1.6 

If you use Maven, go for:

<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. I have no personal experience with it, but according documentation you can change value of Source/Binary Format (to JDK 6 in this case).

You should compile your source code targeted for example at a Java 6 runtime. Using Ant, it's achieved using the target attribute to the javac task. Using Maven, use the Maven Compiler Plugin .

You need to set your compilation targets to JDK 6 (or 5, or whatever is the lowest version you want to support).

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.

You cannot, in general, run a Java 7 file with the Java 6 runtime. You will have to either install Java 7, or rebuild the jar under Java 6.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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