wibarr
wibarr

Reputation: 276

Ant buildfile not able to put jar file in classpath when running other jar file

I'm new to Ant buildfiles, and I've managed to set up my buildfile to create my build directory structure, compile all my files, and jar them into an jar with a manifest specifying the main class correctly.

However, I'm also trying to put into the buildfile the capability to run the jar I just created. It will attempt to run, but I encounter this error:

run:
 [java] Exception in thread "main" java.lang.NoClassDefFoundError: edu/course/lab/pkg3/Lab31

 [java]     at edu.school.oad.lab.pkg1.Main.<init>(Unknown Source)
 [java]     at edu.school.oad.lab.pkg1.Main.main(Unknown Source)
 [java] Caused by: java.lang.ClassNotFoundException: edu.course.antlab.pkg3.Lab31
 [java]     at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
 [java]     at java.security.AccessController.doPrivileged(Native Method)
 [java]     at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
 [java]     at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
 [java]     at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)

 [java]     at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
 [java]     ... 2 more
 [java] Java Result: 1

So I know this means that when I try to run the jar file, the jar file is structured properly and the manifest is set up correctly, but it cannot find the class Lab31 because it is not included in that particular jar file. Instead, it is contained within another jar file located in my library folder, ./lib/ . I tried using the same method to set the classpath for running the jar file the same way that I set the classpath for the actual compilation, but that doesn't seem to be working.

Here's what I have for the run portion of the buildfile:

<target name="run" depends="init, prepare">
    <java jar="${build}/jar/AntLabRun.jar" fork="true">
       <classpath>
          <pathelement location="${lib}/resources.jar" /> 
       </classpath>
    </java>
</target>

My only thought would be that the jar file is thinking the resources.jar file is located at ${build}/jar/${lib}/resources.jar instead of just ${lib}/resources.jar, but if that's the case I'm still not sure how to fix it. Can anyone offer some guidance?

Upvotes: 6

Views: 2114

Answers (2)

Paŭlo Ebermann
Paŭlo Ebermann

Reputation: 74800

Alternatively, you also could add the jars of your class path (i.e. resources.jar) into the Class-Path attribute of the jar manifest. Then your jar is also runnable with -jar outside of ant.

I have something like this in the build file:

<pathconvert property="server.libpath" refid="libpath" pathsep=" ">
  <map from="${dir.libs}" to="../libs/"/>
</pathconvert>
<mkdir dir="${jars}" />
<jar destfile="${jars}/standalone.jar">
  <!-- all the filesets -->
  <manifest>
    <attribute name="Built-By" value="${user.name}" />
    <attribute name="Main-Class" value="de.fencing_game.gui.Lobby" />
    <attribute name="Class-Path" value="${server.libpath}" />
  </manifest>
</jar>

If you only have one file in your path, you don't need the <pathconvert>, but can add it directly in the <attribute>. The path has to be relative from the location of your main jar file, though.

Upvotes: 5

J&#246;rn Horstmann
J&#246;rn Horstmann

Reputation: 34044

The java task probably behaves the same way as the java command line. When both jar and classpath options are given, the classpath is ignored, instead the jar is expected to contain a manifest listing the needed libraries. I would suggest removing the jar attribute, specifying both jars as classpath elements and adding the classname attribute to the java task with the name of the main class.

Upvotes: 5

Related Questions