views:

58

answers:

4

Hi, I have a problem deploying compiled classes in a Tomcat web application: I'm deploying a class which is to be called from a servlet, but when I run the application it fails telling me of a ServletException: Error allocating the servlet instance due to an UnsupportedClassVersionError: Bad version number in .class file.

Tomcat is using Java 1.5.0_06 as reported by the manager. My class was compiled using Java 1.6.0_14. Running javap on any of the classes already present tells me "Major version 46, minor version 0" which should be 1.2.0 initial and which isn't available anymore for download. The oldest I can find is 1.2.1_004 which doesn't even compile.

Do i need to match my Java version to the Tomcat environment or to the classes already there? Re-compiling the whole project using more modern Java is not feasible for me at the moment, although I'd love to do so.

+4  A: 

That's easy: You compiled your application with a later version Java compiler than the Java runtime underneath Tomcat.

Update

The java compiler, javac, supports the options

-source release
    Specifies the version of source code accepted. The following values for release are allowed:
    1.3     the compiler does not support assertions, generics, or other language features introduced after JDK 1.3.
    1.4     the compiler accepts code containing assertions, which were introduced in JDK 1.4.
    1.5     the compiler accepts code containing generics and other language features introduced in JDK 5. The compiler defaults to the version 5 behavior if the -source flag is not used.
    5   Synonym for 1.5

...and even more importantly,

-target version
    Generate class files that will work on VMs with the specified version. The default is to generate class files to be compatible with the JDK 5 VM. When the -source 1.4 or lower option is used, the default target is 1.4. The versions supported by javac are:
    1.1     Generate class files that will run on VMs in JDK 1.1 and later.
    1.2     Generate class files that will run on VMs in JDK 1.2 and later, but will not run on 1.1 VMs.
    1.3     Generate class files that will run on VMs in JDK 1.3 and later, but will not run on 1.1 or 1.2 VMs.
    1.4     Generate class files that will run on VMs in JDK 1.4 and later, but will not run on 1.1, 1.2 or 1.3 VMs.
    1.5     Generate class files that are compatible only with JDK 5 VMs.
    5   Synonym for 1.5

... which will allow you to compile code for a certain version of the JVM.

In other words, you can continue to use your 1.6 compiler, just throw these options at it and you can make it generate 1.5 code which Tomcat will be able to handle.

Carl Smotricz
It's like he answered his own question!
Zoidberg
Don't know about you, but I need all the help I can get :) I'm ecstatic to finally run across a question I'm actually competent to answer.
Carl Smotricz
Fantastic, thanks! I'll try that right away. And yes, I also need all the help I can get :D
Silma
A: 

Do i need to match my Java version to the Tomcat environment or to the classes already there?

You need to ensure that your code is compiled with a version (less than or equals) supported by the JVM you are using; but no, this doesn't need to be the same version that the Tomcat code base was built from - the two code bases can be jvm version independent of each other, as long as they are both supported by the jvm you are using.

Joel
A: 

Instead of recompiling your source to be 1.5 compatible you could update the JDK that Tomcat is using to 1.6.

You should be able to change this by setting the JAVA_HOME environment variable to point to a Java 1.6 install.

pjp
I'd love to update everything to newer and better version, but I don't have permission to do so. :/
Silma
A: 

I got the same UnsupportedClassVersionError issue a while ago. The root cause there was not my own compiled code, but some libraries that were needed, which were compiled with a newer JDK.

AJK