I hava class compiled using jdk7 and I am trying to invoke using JDK6 and the jars in classpath are compiled using jdk 6
jdk6/bin/java com.test.myclass -classpath abc.jar xyz.jar pqr.jar MNO.jar
my class com.test.myclass is present in MNO.jar and abc.jar xyz.jar pqr.jar are compiled with jdk6
with jdk7 it works
jdk7/bin/java com.test.myclass -classpath abc.jar xyz.jar pqr.jar MNO.jar
This also fails.Any input to solve this problem.?
Exception in thread "Main Thread" java.lang.UnsupportedClassVersionError: com/test/MyClass: unsupported classversion 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.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: com.test.MyClass. Program will exit.
256
Thanks
When you compile the sources, give the parameter -target 1.6. That way 1.6 compliant bytecode will be generated (i.e. class files will indicate that they need 1.6 or higher to run).
As the last resort you can change the class version at .class file from 33 to 32 (7-th byte from the file beginning) with a hex editor. If the class does not use any Java 7 new feature it will work OK.
If you have used any jdk 7 specific code in your class then it won't work otherwise it should work.
Related
I have got java 1.7 already installed in my computer(Ubuntu 12.04) and with Java_Home and path properly set.Now I want to install java 1.6 as well because one application I want to use runs only with java1.6.
Here is what I have done so far,
1: I download jdk1.6 and extracted it.
2: Modified the java_home and path to include to point to jdk 1.6.
3: Ran these update-alternative commands
sudo update-alternatives --set java /usr/local/java/jdk1.6.0_45/bin/java
sudo update-alternatives --set javac /usr/local/java/jdk1.6.0_20/bin/javac
Now when I do java -version, I see correct version and I can also compile properly but I do java className I get the following error
Exception in thread "main" java.lang.UnsupportedClassVersionError: three : 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)
My path variable looks like this
/usr/local/bin:/usr/bin:/bin:/usr/games:/usr/local/java/jdk1.7.0_40/bin:/usr/local/java/jre1.7.0_40/bin:/usr/local/java/jdk1.6.0_45/bin:/usr/local/java/jdk1.6.0_45/bin
I read somewhere that if java 1.7 comes before 1.6 in path, there will be a conflict. Could that be the case? In that case,how to remove that from path variable?
you have both jdk 1.6 and 1.7 in your path remove the older version from the path
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Exception in thread “main” java.lang.UnsupportedClassVersionError: a (Unsupported major.minor version 51.0)
My Java program which I run on Mac & Windows is not working. Instead I get this error:
Exception in thread "main" java.lang.UnsupportedClassVersionError: menus/Main_Screen : 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)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
Does anyone know how to fix this? Thanks.
Class version 51.0 is generated by Java 7.
You're trying to run with an earlier version, probably Java 6.
You need to call the Java 7 version of the java command.
The version used to run NetBeans is set in NetBeans/etc/netbeans.conf.
The version used to build and run a project is set by the project properties.
You can tell NetBeans about a new Java version through the Tools > Java Platforms
Unsupported major.minor version 51.0 at java.lang.ClassLoader.defineClass1(
It seems your java runtime version is different (lower) version from compile time version.
The general rule is this.
If take a *.class compiled by a version of Java EARLIER than the JRE, it should work in that JRE.
Because the later JRE is supposed to "know" previous versions of format.
If take a *.class compiled by a version of Java LATER than the JRE, it should not work in that JRE.
Because the earlier JRE is not supposed to know the future versions of format.
In your case the code is simply compiled by the version of Java issued after the version of the JRE.
I've seen other questions relating to this exact error, but they don't seem to help at all.
I'm coding an app to run on a Linux system (CentOS if I'm not mistaken), which has Java 1.5 version. I am currently editing the program on my Eclipse IDE on my Mac OSX system which has Java 1.6.
I compiled the project into a runnable .JAR file. When prompted with the option of what to do with the libraries in Eclipse, I choose "extract selected packages into .JAR file".
I know Eclipse has its own compiler, so in the project settings I changed the target compiler to Java 1.5 and rebuilt. And then repackaged the runnable jar file.
However, when running the .JAR file with "java -jar file.jar" on the target machine, I still get the aforementioned "Bad version number" error.
Can somebody help?
Full error is:
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
I am using only using one external library, sun-javamail.
Thank you
You either:
haven't recompiled everything with -target 1.5
haven't redeployed everything
are using third party JARs compiled for 1.6. The class named in the exception will tell you that.
You may want to run javap command on class file and check exact version of JDK for which it was complied.
For example:
C:\>javap -verbose Main
Compiled from "Main.java"
public class Main extends java.lang.Object
SourceFile: "Main.java"
minor version: 0
major version: 50
...
Following is the mapping between major version and jdk
MajorVersion JDK
51 J2SE 7
50 J2SE 6.0
49 J2SE 5.0
Reference: Class file format
I have been coding with Eclipse and testing the code using Terminal on OS X 10.7.4. When I ran the code with Eclipse, it worked. However, running with terminal gave me the following error:
Exception in thread "main" java.lang.UnsupportedClassVersionError: PacketPrinter : 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)
I checked my java and javac version. They are both 1.7. I have been googling but could not find anything.
You're probably not using the same runtime on the command line as when you launch the program from within Eclipse.
Try executing
java -version
from the command line to see which version of java you're using on the terminal.
(You can also run which java to see exactly which file is being executed.)
It seems you have a different version of java running in eclipse and in your terminal.
Do the following:
In the terminal type: java -version
In eclipse goto the preferences and look in Java/Installed JREs
Compare the versions.
Point you $JAVA_HOME and $PATH variables to the same JRE you use in Eclipse.
As aioobe said check you java version using following command
java -version
Copy the contents of the PATH settings to a notepad and check if you have any other java version path present in it and if it is before that of the 7. If so, remove that path in the PATH setting and save it.
I would suggest you to re logging after doing it.
AFAIK this error comes when you have older version java path present in path.
I created a Java application using Netbeans with JDK version as 1.7
Now how do I compile/run the same application on a different machine where JDK 1.5 is installed without making any changes to the code?
When I tried compiling I got an error saying JDK version mismatch.
Given below is the error:
java.lang.UnsupportedClassVersionError: MTPS : 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)
Could not find the main class: MTPS. Program will exit.
Changes from one java version to other have different functionality added. Like for example Enum functionality was added since java 1.5 if you try to compile it on 1.4 you get an error.
btw try to compile your app with JDK 1.5, and then if you succeed try to run it.
You need to take care of functionality change in jdk 1.4 to jdk1.6 and jdk 1.6 to jdk 7.
Generic ,Assertion were new implementation in jdk1.5.Some enhancement in Switch case(with String) and Exception handling is new in JDK7.
Code can be compiled for an earlier JRE using the cross-compilation options of javac. If using a 1.7 JDK & targeting JRE 1.5, a 1.5 version rt.jar should be supplied as the boot class path (see docs. for details).