2

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
TyC
  • 792
  • 6
  • 11
  • 23
  • check this out: http://stackoverflow.com/questions/2466828/java-lang-unsupportedclassversionerror-bad-version-number-in-class-file it's a version conflict – JKirchartz Mar 01 '12 at 18:35

9 Answers9

4

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.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • John does have the most complete answer. Yours is simpler though. (Mine is a hybrid =3… People hate hybrids. :( ) –  Mar 02 '12 at 00:48
4

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
2

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.

1

Make sure you have on both platforms the latest version of Oracle Java installed.

Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
1

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.

Jesper
  • 202,709
  • 46
  • 318
  • 350
1

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.

Michael Smith
  • 1,847
  • 2
  • 19
  • 19
1

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.

1

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

aleroot
  • 71,077
  • 30
  • 176
  • 213
0

(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.

halfer
  • 19,824
  • 17
  • 99
  • 186