Thursday, March 11, 2010

Compiled java class info

Recently I had developed a small web application with Apache Click, Spring  and Cayenne.  I develop usually locally on Tomcat 6.x without much issue.  These days I am running Windows 7 and JDK6+, basically all the latest.  I test on another server Win XP running JBoss 5 on Java 6 as that is closer to the deployment target of (as I found out after development!) Linux with JBoss 5 running on JDK5...I dont even know the Linux build or version I expect it is Redhat of some flavour.

All was fine until I dropped my war file up to the remote JBoss and I start getting the bad class version errors:
java.lang.UnsupportedClassVersionError: Bad version number in .class file
and subsequent stacktraces of NoClassDefFound

;Fair enough, I've compiled them locally on Java 6.  Using maven I have as standard a in my top-level pom.xml the maven-compiler-plugin as a build plugin the setting was target of 1.6 :


Easy enough, replace the 6's with 5's, clean the build and rebuild. "mvn clean install"

I kept getting the error so wondered how I could check the version of the class file?

(Before going on the error was fixed with another clean and rebuild of a dependent project, arghhh)

The answer is "javap".  Run it with "-verbose" and you get more than enough info.  Up the top is the version table.  Mine said 49, which is found in the following version table


major  minor Java platform version 
45       3           1.0
45       3           1.1
46       0           1.2
47       0           1.3
48       0           1.4
49       0           1.5
50       0           1.6


Make sure you run with the short class name as the name parameter not the filename of the class itself.


Run:
javap -verbose DataException


Result:
Compiled from "DataException.java"
public class com.myco.common.data.DataException extends java.lang.Exception
  SourceFile: "DataException.java"
  minor version: 0
  major version: 49
  Constant pool:
const #1 = Method       #5.#25; //  java/lang/Exception."":(Ljava/lang/String;)V
const #2 = Method       #5.#26; //  java/lang/Exception."":()V
const #3 = Field        #4.#27; //  com/myco/common/data/DataException.hiddenException:Ljava/lang/Exception;
const #4 = class        #28;    //  com/myco/common/data/DataException
const #5 = class        #29;    //  java/lang/Exception
const #6 = Asciz        hiddenException;
const #7 = Asciz        Ljava/lang/Exception;;
const #8 = Asciz        ;
const #9 = Asciz        (Ljava/lang/String;)V;
const #10 = Asciz       Code;
const #11 = Asciz       LineNumberTable;
const #12 = Asciz       LocalVariableTable;
const #13 = Asciz       this;
const #14 = Asciz       Lcom/myco/common/data/DataException;;
const #15 = Asciz       msg;
const #16 = Asciz       Ljava/lang/String;;
const #17 = Asciz       ()V;
const #18 = Asciz       (Ljava/lang/String;Ljava/lang/Exception;)V;
const #19 = Asciz       error;
const #20 = Asciz       excp;
const #21 = Asciz       getHiddenException;
const #22 = Asciz       ()Ljava/lang/Exception;;
const #23 = Asciz       SourceFile;
const #24 = Asciz       DataException.java;
const #25 = NameAndType #8:#9;//  "":(Ljava/lang/String;)V
const #26 = NameAndType #8:#17;//  "":()V
const #27 = NameAndType #6:#7;//  hiddenException:Ljava/lang/Exception;
const #28 = Asciz       com/myco/common/data/DataException;
const #29 = Asciz       java/lang/Exception;


{
public com.myco.common.data.DataException(java.lang.String);
  Code:
   Stack=2, Locals=2, Args_size=2

No comments: