I have a Jar which is running in fine in eclipse and cmd prompt.
When i tried executing the same in Unix , it is not executing.
Note: Java version in Unix is 1.5 ; Java version in eclipse is 1.7
java -jar text.jar
Exception in thread "main" java.lang.UnsupportedClassVersionError: Bad
version number in .class file
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:621)
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:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
You answered the question yourself. While compiled at 1.7 you try to execute the jar in an 1.5 environment. Update your Unix JRE to 1.7 and it should work.
Alternatively you can rebuild the jar at 1.5. In eclipse, right click at your project in the project browser => properties => java compiler => compiler compliance level.
You get the UnsupportedClassVersionError when you are trying to run an application having a jar in the classpath that is built using a newer version than the one you are using at runtime.
You get this error when you try to run a class that was compiled for a Java version newer than what you have; for example, if you try to use a class that was compiled for Java 6 or newer on a Java 5 JVM.
It doesn't necessarily have to be your own class; you might be using some library that was built for Java 6 or newer.
Are you using specific libraries (JAR files)? Check if these are compatible with Java 5 or not. Or upgrade your Java version to Java 6
Related
As I try to run the application on localhost, I get this exception. Why does it occur. What do I need to do resolve it ?
Exception in thread "main" java.lang.UnsupportedClassVersionError: com/google/appengine/tools/development/agent/AppEngineDevAgent : 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)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
at sun.instrument.InstrumentationImpl.loadClassAndStartAgent(InstrumentationImpl.java:300)
at sun.instrument.InstrumentationImpl.loadClassAndCallPremain(InstrumentationImpl.java:358)
FATAL ERROR in native method: processing of -javaagent failed
JVMs are backwards compatible, not forward. So a Java 6 JVM can run Java 5 compiled bytecode, but not Java 7. This is what's happening in your case.
Latest AppEngine SDKs require your code to be compiled with Java 7 (which you probably do), then apparently you run it under Java 6 JVM.
This usually happens when your IDE runs on Java 6 JVM - check settings.
The verison of javac and java used have to be same, i.e. the Java version used to compile the code and the one user to run the code have to be same. Check javac and java versions on command prompt.
javac -version
java -version
If you are using an IDE to run your project, check the jdk version in your IDE Preferences.
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've created a executable jar file in an Windows environment. I'm able to run this jar without any problems in Windows.
When I try and run the same jar in a unix environment, I get the following exceptions:
Exception in thread "main" java.lang.UnsupportedClassVersionError: Bad version number in .class file
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:621)
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:307)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:242)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:56)
What am I doing wrong?
You are trying to run it under an earlier version of Java.
You can compile targeting the correct version, or run under a newer version.
Looks like you're building on Java 7 and then trying to run on Java 5, or something like that. It's not a matter of Windows vs Unix - it's a matter of the version of Java within the operating system.
Ideally, upgrade your Unix system to a recent release of Java - or if you can't do that, change how you're building to use the -target flag in javac to generate appropriate bytecode. You should target the right version of the standard libraries for the environment you need to run in, too.
You are trying to run a program compiled on a newer version of Java on an older version of Java.
Make sure the program is compiled for the appropriate target version that you will be running on. You can set compile options to use a newer compiler to create programs that will run on older versions, but obviously you can't use newer features.
For example: Java 7 runs all previous versions of binaries. Java 5 can't run newer versions of binaries.
Make sure you have on both platforms the latest version of Oracle Java installed.
UnsupportedClassVersionError means that you compiled the class with a version of Java that is newer than the version of Java that you are trying to run the program on.
For example, you have compiled this with JDK 6, and you are trying to run the program on a Java 5 or older JVM.
That doesn't work. Newer versions of Java are able to run programs compiled with older versions, but not the other way around.
It's likely that you have an older version of Java on your *nix install as opposed to your Windows install. The jar was built using a newer version, meaning it can't run on the old version because it may have features that aren't supported yet. Try updating your *nix Java and try running it again.
It looks like the .class file was compiled with a newer version of java then your linux installation. please update the results of:
java --version
on both linux and windows, and check if you have java, and not openjdk, on linux (openjdk might bug).
if the version on linux is lower then on windows, that is the problem, update it.
Most likely you are trying to execute a jar file compiled with a compiler with a newer version than the JVM installed on the system where you are trying to execute it. Check JVM version on the Linux system and make sure to have a version equal or newer than the version of the compiler ...
(Posted on behalf of the question author).
Thanks for the help, it seems you all are correct. I developed the jar on java 1.6 and our unix server is on 1.5.
I've made this application in a Windows environment. In eclipse, I made an executable .jar file by going to File > Export > Executable Jar
I can launch the app in Windows by openening it with Java. However in Linux, when I run java -jar app.jar, I get this:
Exception in thread "main" java.lang.UnsupportedClassVersionError: ConfigReader (Unsupported major.minor version 50.0)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:539)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:123)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:251)
at java.net.URLClassLoader.access$100(URLClassLoader.java:55)
at java.net.URLClassLoader$1.run(URLClassLoader.java:194)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:187)
at java.lang.ClassLoader.loadClass(ClassLoader.java:289)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:274)
at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)
What does unsupported class version error mean?
The application was probably written in Java 6 and you are trying to run it in Java 5. You can either build it under Java 5 in eclipse or install Java 6 onto your linux machine
The version that the jar was compiled in is higher than the version you are trying to run in.
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/UnsupportedClassVersionError.html
java.lang.UnsupportedClassVersionError: Bad version number in .class file?
It means that the JAR file was compiled to a version of the JVM bytecode that the currently installed JVM cannot run.
Odds are excellent that the software compiled on a newer version of Java, but are attempting to run it on an older version.