3

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?

asked Aug 22, 2012 at 20:43
6
  • 2
    What's the error? It could very well be that you need the latest JDK to run it, period. Commented Aug 22, 2012 at 20:44
  • 2
    Exception 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.access000ドル(URLClassLoader.java:73) at java.net.URLClassLoader1ドル.run(URLClassLoader.java:212) ...... Could not find the main class: tes. Program will exit. Commented Aug 22, 2012 at 20:46
  • the error looks like this in the above comment Commented Aug 22, 2012 at 20:47
  • 1
    You 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) Commented Aug 22, 2012 at 20:49
  • Back in the old days of Java 1.4 you could use Retroweaver to run 1.5 code on a 1.4 JRE. Too bad it doesn't work anymore... Commented Aug 22, 2012 at 20:50

5 Answers 5

3

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.

Arjan Tijms
38.2k12 gold badges112 silver badges144 bronze badges
answered Aug 22, 2012 at 23:58
Sign up to request clarification or add additional context in comments.

Comments

1

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

answered Aug 22, 2012 at 20:45

2 Comments

what is the solution how can I change my code to be able to run it in an older version
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.
1

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.

answered Aug 22, 2012 at 23:11

Comments

0

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
answered Aug 22, 2012 at 20:44

9 Comments

what is the commend ? can I write javac -source 1.6 code.jar??
@user1475288: It is not source, it is target. Read the link provided in my answer.
but if my file is "yourfile.jar" can I use javac file.jar -target 1.6?
I don't think you can use this against jar.
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
|
0

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.

answered Apr 9, 2016 at 14:42

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.