I have a code.jar running well in 1.7 version java, but when using another computer with linux with version 1.6 it is giving me an error, how can I run it in 1.6 version?
-
2What's the error? It could very well be that you need the latest JDK to run it, period. – Philippe Aug 22 '12 at 20:44
-
2Exception in thread "main" java.lang.UnsupportedClassVersionError: tes : Unsupported major.minor version 51.0 at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(ClassLoader.java:634) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) at java.net.URLClassLoader.defineClass(URLClassLoader.java:277) at java.net.URLClassLoader.access$000(URLClassLoader.java:73) at java.net.URLClassLoader$1.run(URLClassLoader.java:212) ...... Could not find the main class: tes. Program will exit. – user1475288 Aug 22 '12 at 20:46
-
the error looks like this in the above comment – user1475288 Aug 22 '12 at 20:47
-
1You need to create the .class files using a JDK 1.6 or use the `-target=1.6` option when compiling (but that won't help if you are using Java7 features) – Aug 22 '12 at 20:49
-
Back in the old days of Java 1.4 you could use [Retroweaver](http://retroweaver.sourceforge.net) to run 1.5 code on a 1.4 JRE. Too bad it doesn't work anymore... – maba Aug 22 '12 at 20:50
-
Do I need to uninstall the JDK 1.7 ? – user1475288 Aug 22 '12 at 20:52
5 Answers
Use the cross-compilation options when invoking javac
.
If only the -target
option is specified as recommended in most other replies, the 1.7 SDK will issue a warning about -bootclasspath
. That option requires an rt.jar
of the target JRE version (note not JDK). It allows the compiler to check that the classes, methods and attributes of the code that reference core Java SE are actually present in the rt.jar
. This is very important to ensure compatibility.

- 37,782
- 12
- 108
- 140

- 168,117
- 40
- 217
- 433
If the code is not compiled with an older Java compatibility flag, you won't be able to run it on 1.6.

- 32,246
- 5
- 63
- 79
-
what is the solution how can I change my code to be able to run it in an older version – user1475288 Aug 22 '12 at 20:48
-
First make sure the source code that ends up in code.jar is compilable with Java 1.6. Then, when you compile it with javac, use the -target 1.6 flag. – Dan D. Aug 22 '12 at 20:50
Build the project using Java 6 in eclipse, you will need to download Java 1.6 JRE, then have eclipse point to 1.6 JRE, see: http://www.cleartoolkit.com/dokuwiki/doku.php?id=clearwiki:20.cleardatabuilder:01.setup:01.prerequisites
Now right click on your project -> Properties, click Java Compiler, uncheck "enable project specific settings", click "Configure Workspace Settings" , and select 1.6 as your compiler compliance level.
Do "the thing" to export your project again, the new jar should work.
Hope this helps.

- 320
- 3
- 19
I don't think you can run higher version class with lower version.
Another approach may be while compiling set target version as 1.6.
Example:
javac yourFile.java -target 1.6

- 65,990
- 13
- 130
- 167
-
-
@user1475288: It is not source, it is target. Read the link provided in my answer. – kosa Aug 22 '12 at 20:53
-
but if my file is "yourfile.jar" can I use javac file.jar -target 1.6? – user1475288 Aug 22 '12 at 20:56
-
-
I wrote the code using eclipse, then I made the export thing so that I can use it outside eclipse, but the version of java is 1.7 on my computer – user1475288 Aug 22 '12 at 21:00
-
If you are doing it in eclipse, set compiler version to 1.6 in eclipse. Everything taken care. – kosa Aug 22 '12 at 21:00
-
how i can set the compiler version to 1.6 in eclipse I'm new to eclipse :S – user1475288 Aug 22 '12 at 21:06
-
see the screenshot in this link, go to Java compiler and change it to 1.6 http://stackoverflow.com/questions/4638108/how-do-i-configure-eclipse-to-make-use-of-different-version-of-jdk?rq=1 – kosa Aug 22 '12 at 21:08
-
I have a question not related to the subject, if I was opening a terminal session with a server and running a code on the server, then I close this session, does the code stops running? – user1475288 Aug 23 '12 at 06:52
I have found a solution within Eclipse!
If you go to your project and right click to access the properties, there is a tab called Java Compiler. If you enable project specific properties and unclick the check box under JDK compliance, you can change the source compatibility and .class files compatibility to be 1.6.
This should fix your problem and the generated .jar file should work on both devices. I tested this by moving a file from 1.8 to 1.7 and testing on my Raspberry Pi that has OpenJDK 1.7 which worked great. I hope this helps.

- 1
- 2