comatose
comatose

Reputation: 1962

Java NoClassDefFoundError for the InnerClass

I am trying to execute a Java class from ant. I first create a jar file for my class and then execute it via ant target. But it is throwing me this error :

Exception in thread "main" java.lang.NoClassDefFoundError: com/abc/utils/ClassName$InnerClass

I am creating a jar in the build file like this:

<jar destfile="${dist.dir}/../lib/jarFile.jar" basedir="${basedir}/classes" includes="com/abc/utils/ClassName.class" />

If I remove the "includes" during the creating of the jar, then everything is fine and I am able to execute the jar file, but I don't want to include all the files in the jar, since I only need this one class.

Any ideas how to fix this?

Upvotes: 0

Views: 1858

Answers (3)

pb2q
pb2q

Reputation: 59607

It looks like you're using an inner class in class ClassName. This will produce a separate class file.

Because you're using the includes attribute, the jar task is specifically only including a single class file: ClassName.class. Remove the includes, and all class files will end up in your jar.

Note that includes/excludes will also accept a comma-separated or space-separated list, e.g.:

<jar destfile="${dist.dir}/../lib/jarFile.jar" basedir="${basedir}/classes" includes="com/abc/utils/ClassName.class,com/abc/utils/ClassName$InnerClass.class" />

This or a similar list should address your issue if you want to only include ClassName and any inner classes or additional dependencies in the jar.

Upvotes: 2

kosa
kosa

Reputation: 66637

If it is inner class, may be you need to do enclosingclass$className.class because your innerclass will be created in separate file with $ symbol appended.

Note: If another class depend on this class, runtime you may see issues.

Upvotes: 1

Brian Agnew
Brian Agnew

Reputation: 272217

That inner class will be a separate file (called ClassName$InnerClass.class, I think), and you're explicitly excluding it in your jar definition.

Inner classes will manifest themselves as separate class files in the filesystem. Hence just including that one .class file unfortunately will break your deployable, as it's missing the corresponding inner class.

Upvotes: 3

Related Questions