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.
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.
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...
I guess you have compiled with java version 6, Please re-check
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);
}
}