0

I have taking an existing, old, Java code base and changed one class. I have recompiled the code base in Java 1.5.0. I then successfully deploy this code on Tomcat.

To test my deployment, I send a message to my application and I hit an error with the class that I changed.

Error loading class [com.MyClass] for bean with name 'myClass' defined in ServletContext resource [/WEB-INF/ApplicationContext.xml]: problem with class file or dependent class; nested exception is java.lang.UnsupportedClassVersionError: (com/MyClass) bad major version at offset=6

I have checked the deployed Ear and War's manifest files and both have 1.5.0_22-b03. The version on the Tomcat instance is 1.5.0.

Can anyone tell me why I'm getting this error? The JDK running the application is the same version that the code was compiled on.

Strangely, the only class that has the error is the class I amended.

TheCoder
  • 8,413
  • 15
  • 42
  • 54
  • 1
    You must have used Eclipse of NetBean IDE for changing that java class right? And there are chances that JDK used by your eclipse is other then what is there for your Tomcat. you can check it by going to build path of your project – mitpatoliya Sep 11 '13 at 08:30
  • Your JDK versions notwithstanding, the error very clearly indicates that you've compiled that class with >1.5 or your Tomcat is running on a JRE < 1.5.. – Anders R. Bystrup Sep 11 '13 at 08:30

4 Answers4

1

You have compiled your class using Java 6 and deploying application with lower version i.e. Java 5. Use Java 6 run time it will fix your problem.

“Caused by: java.lang.UnsupportedClassVersionError: (myclassname) bad major version at offset=6”

This error indicates that your projects were compiled with a higher level Java compiler than the runtime can support.

Macrosoft-Dev
  • 2,195
  • 1
  • 12
  • 15
0

Seems as you have compiled your code with a higher Java version than 1.5. You should check if you have the right JRE installed (Window -> Preferences -> Java -> Installed JREs).

You should also check if the Compiler compliance level is set to 1.5 (Window -> Preferences -> Java -> Compiler).

I'm assuming you are using Eclipse...

Kai
  • 38,985
  • 14
  • 88
  • 103
  • I edited the file in Intellij but compiled it and built the Ear on the command line. When I run java -version on the cmd line, I get 1.5.0_22-b03. – TheCoder Sep 11 '13 at 08:40
  • Maybe intellij compiled that class before to your bin folder and it wasn't rebuild from the command line because it already was up to date? Make sure to recompile all classes. Anyway how are you creating the ear? – Kai Sep 11 '13 at 08:48
  • Uses your machine and the servers tomcat both Oracle JDKs? Or does the server maybe run an OpenJDK or an IBM? Maybe that can cause the trouble also. – Kai Sep 11 '13 at 08:52
  • I have a build file so create the Ear using Ant via the command line. – TheCoder Sep 11 '13 at 09:05
  • Maybe the ant file uses a different JDK than your `java -version` from the path. – Kai Sep 11 '13 at 09:12
  • I've checked and Ant uses Java_home which is 1.5. I've removed 1.5 and 1.6 from my machine and readded 1.5. I've recompiled the Ear and will see if it works. The manifest says 1.5 anyway. – TheCoder Sep 11 '13 at 09:33
  • Isn't Intellij JDK irrelevant if I am using ant to build. I have made a new amendment in my class to force a recompile of the class. I've also done a clean. I will redeploy now. – TheCoder Sep 11 '13 at 09:41
  • Yes it should. But I really don't know what your ant script does. Maybe it downloads a JDK 7 and compiles your code with that. I can't tell. – Kai Sep 11 '13 at 09:58
  • I've noticed my ant build file references a file called J2EE.jar. Would that possibly mean it's using Java 1.6? The manifest file in the j2ee.jar file says Java 1.4. – TheCoder Sep 11 '13 at 10:18
  • I removed 5 + 5, readded 5, recompiled and redeployed. I suspect I was actually compiling in 6. Problem solved. Thanks. – TheCoder Sep 11 '13 at 11:06
0

I guess you have compiled with java version 6, Please re-check

upog
  • 4,965
  • 8
  • 42
  • 81
0

As you probably run java indirectly from the command line, ant or maven, it depends on their java, javac and build scripts, and variables therein. Maven is uncomplicated; in Ant you can echo the java version should there be many includes.

To check the .class file, try the following. It is Java 7, but easily translatable to an earlier version.

private static void dumpJavaClassVersion(String path) throws IOException {
    File file = new File(path);
    try (InputStream in = new FileInputStream(file)) {
        byte[] header = new byte[8];
        int nread = in.read(header);
        if (nread != header.length) {
            System.err.printf("Only %d bytes read%n", nread);
            return;
        }
        if (header[0] != (byte)0xCA || header[1] != (byte)0xFE
                || header[2] != (byte)0xBA || header[3] != (byte)0xBE) {
            System.err.printf("Not a .class file (CAFE BABE): %02X%02X %02X%02X",
                    header[0], header[1], header[2], header[3]);
            return;
        }
        int minorVs = ((header[4] & 0xFF) << 8) | (header[5] & 0xFF);
        int majorVs = ((header[4] & 0xFF) << 8) | (header[5] & 0xFF);
        final String[] versionsFrom45 = {"1.1", "1.2", "1.3", "1.4", "5", "6", "7", "8", "9", "10"};
        int majorIx = majorVs - 45;
        String majorRepr = majorIx < 0 || majorIx >= versionsFrom45.length ? "?" : versionsFrom45[majorIx];
        System.out.printf("Version %s, internal: minor %d, major %d%n",
                majorRepr, minorVs, majorVs);

    }
}
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138