3

I'm attempting to add Log4j to my project's classpath in Ant which creates an executable JAR, but it appears that it's not being added properly.

Here is the path component of my Ant build script:

<path id="classpath.compile">
  <fileset dir="${dir.myLibs}">
    <include name="**/*.jar"/>
  </fileset>
  <pathelement location="${dir.webContent}/WEB-INF/lib/log4j.jar" />
</path>

The compile target looks like this:

<target name="-compile">
  <javac destdir="${dir.binaries}" source="1.6" target="1.6" debug="true" includeantruntime="false">
    <src path="${dir.source}"/>
    <classpath refid="classpath.compile"/>
  </javac>
</target>

Tthe target that creates the JAR:

<target name="-createJar" >
  <jar jarfile="${path.jarFile}"
     manifest="${dir.source}\META-INF\MANIFEST.MF">
    <fileset dir="${dir.binaries}" casesensitive="yes">
      <exclude name="**/*.java"/>
    </fileset>
  </jar>
</target>

Lastly, the MANIFEST.MF:

Manifest-Version: 1.0
Class-Path: ../../../WebContent/WEB-INF/lib/log4j.jar (what is this pathing relative to?)
Main-Class: foo.Bar

The JAR is created, but when I execute it, I get:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger...

Any thoughts as to what I'm doing wrong?

Tyler Treat
  • 14,640
  • 15
  • 80
  • 115

3 Answers3

1

I'm guessing that you're running the Java program as follows

java -jar myapp.jar

In this case you'll need to specify the Class-Path attribute in the manifest. I suggest you also check out the manifestclasspath task

Mark O'Connor
  • 76,015
  • 10
  • 139
  • 185
1

Creating the jar does not include the linked libraries in the jar. You would have to have the required jars in your execution classpath in order to run it that way. Or, you could use the solution I use, which is to create a one-jar archive. It adds a specialized class loader for your application into the resulting jar and also packages your required jars in to the final executable jar. It works really well for deploying neat, simple to use packages.

Deven Phillips
  • 1,129
  • 14
  • 39
1

It looks from the classpath in your MANIFEST that you are trying to reference a jar inside your jar. The only two ways to make that work AFAIK are 1) a special classloader, like @infosec812 mentions, or 2) by exploding the jar dependencies directly into the root of your jar. Either is workable, but I don't see either of them happening in your ant script.

If you're trying to reference a jar outside of your jar, your relative classpath is relative to the location of the jar you are executing. Make sure the referenced jar exists in that location.

highlycaffeinated
  • 19,729
  • 9
  • 60
  • 91