Debugging ant UnsupportedClassVersionError
I'm getting the following exception trace as the result of running ant package
(this is supposed to create a JBoss .ear
package):
$ ant package
Buildfile: /usr/local/src/appname/build.xml
init:
[echo] Classpath = ${java.classpath}
build:
package:
BUILD FAILED
/usr/local/src/appname/build.xml:369: 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 org.apache.tools.ant.util.depend.bcel.AncestorAnalyzer.<init>(AncestorAnalyzer.java:45)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.n开发者_如何学运维ewInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:501)
at java.lang.Class.newInstance0(Class.java:350)
at java.lang.Class.newInstance(Class.java:303)
at org.apache.tools.ant.taskdefs.optional.ejb.GenericDeploymentTool.createAnalyzer(GenericDeploymentTool.java:281)
at org.apache.tools.ant.taskdefs.optional.ejb.GenericDeploymentTool.configure(GenericDeploymentTool.java:308)
at org.apache.tools.ant.taskdefs.optional.ejb.EjbJar.execute(EjbJar.java:582)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:592)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.Target.execute(Target.java:390)
at org.apache.tools.ant.Target.performTasks(Target.java:411)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1360)
at org.apache.tools.ant.Project.executeTarget(Project.java:1329)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
at org.apache.tools.ant.Project.executeTargets(Project.java:1212)
at org.apache.tools.ant.Main.runBuild(Main.java:801)
at org.apache.tools.ant.Main.startAnt(Main.java:218)
at org.apache.tools.ant.launch.Launcher.run(Launcher.java:280)
at org.apache.tools.ant.launch.Launcher.main(Launcher.java:109)
Total time: 0 seconds
Is there any way to find out which exactly class file it was trying to load when this exception occurred?
I've tried running with ant -v
and ant -debug
, but that doesn't add any valuable information about the problem.
As one can see, ant build
finishes w/o errors (and also ant clean
works.)
I understand something is broken in my setup, but knowing the offending class file name would be of great help to fixing the setup.
Ant and java/javac versions:
$ ant -version
Apache Ant version 1.8.0 compiled on March 11 2010
$ java -version
java version "1.5.0_22"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_22-b03)
Java HotSpot(TM) 64-Bit Server VM (build 1.5.0_22-b03, mixed mode)
$ javac -version
javac 1.5.0_22
OS version:
$ lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux 6.0.2 (squeeze)
Release: 6.0.2
Codename: squeeze
JBoss is installed to /opt/jboss-4.2.3.GA
.
Any pointers?
According to Javadocs for java.lang.UnsupportedClassVersionError
the point of this class is:
Thrown when the Java Virtual Machine attempts to read a class file and determines that the major and minor version numbers in the file are not supported.
This tells me that there is a class that is at an unsupported version (usually read this as as "higher version" than what your Ant is using at the moment.
So...that being said, and seeing that your Ant script looks like it is trying to do something possibly with EJB stub generation or of the like, makes me wonder what version of Java EE you are trying to work with and what version of JBoss you are working with. Find out that (like EE 6 isn't Java SE 5) and what JRE JBoss is using. If you are using any jars to package that are at a level higher than 1.5 build 22 (even if it is 1.5 build 23), then you will probably run into this error. Make sure all your jars (even third party jars and libraries and what not that you are using to generate your Java EE app) are all at 1.5 build 22 or lower. That should help alleviate the problem. If you are unsure about a jar's version, you can always "open" it using Winzip and look at its MANAFEST.MF file.
The problem was that ant
was installed from Debian repo, while the rest of the java packages: such as jdk, jre, jboss, etc. from other vendors (Sun/Oracle, Apache, etc.)
I've removed Debian-shipped ant and replaced it with the latest binaries from Apache. This solved my problem.
I have faced the same issue. I found resolution to this was in the ant script. Inside ant script where it is compiling classes I specified the build.target parameters. Example:
<target name="compile"><javac compiler="javac1.5" target="1.5" ....></target>
精彩评论