0

I'm running an Ant script for generating a JAR file but when I excecute the JAR File I get following error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger
        at mx.isban.csd.ws.Main.<clinit>(Main.java:31)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 1 more

this is my ant script:

<?xml version="1.0" encoding="UTF-8"?>
<project name="WSCargaLlavesClient" default="build" basedir="..">
  <description>Build File</description>

  <property name="app.jar.cli"   value="${ant.project.name}.jar" />
  <property name="dir.src.cli"   value="${basedir}/${app.jar.cli}"/>
  <property name="dir.build"     value="${basedir}/build"/>
  <property name="dir.lib"       value="${basedir}/lib"/>
  <property name="classes.dir"   value="${dir.build}/classes"/>


  <path id="classpath">
    <fileset dir="${dir.lib}" includes="**/*.jar"/>
  </path>

  <target name="clean">
    <delete dir="${dir.build}"/>
  </target>

  <target name="compila-client">
    <mkdir dir="${dir.lib}"/>
    <mkdir dir="${dir.build}"/>
    <mkdir dir="${classes.dir}"/>
    <javac srcdir="${dir.src.cli}" debug="on" destdir="${classes.dir}" 
           classpathref="classpath" includeantruntime="false"
           source="1.6" target="1.6"/>
  </target>

  <target name="build" depends="compila-client">
    <jar destfile="${dir.build}/${app.jar.cli}" basedir="${classes.dir}">
      <manifest>
        <attribute name="Class-Path" value="lib/commons-beanutils.jar lib/commons-codec-1.8.jar lib/commons-io-1.4.jar lib/commons-lang.jar lib/commons-logging.jar lib/ezmorph-1.0.2.jar lib/json-lib-1.1.jar lib/log4j-1.2.17.jar" />
        <attribute name="Main-Class" value="mx.isban.csd.ws.Main"/>
      </manifest>
      <fileset dir="." includes="lib\*"/>
    </jar>
  </target>

  <target name="clean-build" depends="clean,build"/>

</project>

I already opened my manifest and I found the log4j-1.2.17.jar file

enter image description here

I also checked my MANIFEST.MF file and there it is: enter image description here

When I generate a Runnable JAR file with eclipse it works just fine, this is the ANT script generated by eclipse:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project default="create_run_jar" name="Create Runnable Jar for Project WSCargaLlavesClient with Jar-in-Jar Loader">
    <!--this file was created by Eclipse Runnable JAR file Export Wizard-->
    <!--ANT 1.7 is required-->
    <!--define folder properties-->
    <property name="dir.buildfile" value="."/>
    <property name="dir.workspace" value="C:/Users/Jesus Ayala/developer/workspace/WSCargaLlavesClient"/>
    <property name="dir.jarfile" value="${dir.buildfile}"/>
    <target name="create_run_jar">
        <jar destfile="${dir.jarfile}/WSCargaLlavesClient.jar">
            <manifest>
                <attribute name="Main-Class" value="org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader"/>
                <attribute name="Rsrc-Main-Class" value="mx.isban.csd.ws.Main"/>
                <attribute name="Class-Path" value="."/>
                <attribute name="Rsrc-Class-Path" value="./ commons-beanutils.jar commons-codec-1.8.jar commons-io-1.4.jar commons-lang.jar commons-logging.jar ezmorph-1.0.2.jar json-lib-1.1.jar log4j-1.2.17.jar"/>
            </manifest>
            <zipfileset src="jar-in-jar-loader.zip"/>
            <fileset dir="${dir.workspace}/WSCargaLlavesClient/bin"/>
            <zipfileset dir="C:/REPO/SellosDigitales-Des/src/java/WSCargaLlavesClient/lib" includes="commons-beanutils.jar"/>
            <zipfileset dir="C:/REPO/SellosDigitales-Des/src/java/WSCargaLlavesClient/lib" includes="commons-codec-1.8.jar"/>
            <zipfileset dir="C:/REPO/SellosDigitales-Des/src/java/WSCargaLlavesClient/lib" includes="commons-io-1.4.jar"/>
            <zipfileset dir="C:/REPO/SellosDigitales-Des/src/java/WSCargaLlavesClient/lib" includes="commons-lang.jar"/>
            <zipfileset dir="C:/REPO/SellosDigitales-Des/src/java/WSCargaLlavesClient/lib" includes="commons-logging.jar"/>
            <zipfileset dir="C:/REPO/SellosDigitales-Des/src/java/WSCargaLlavesClient/lib" includes="ezmorph-1.0.2.jar"/>
            <zipfileset dir="C:/REPO/SellosDigitales-Des/src/java/WSCargaLlavesClient/lib" includes="json-lib-1.1.jar"/>
            <zipfileset dir="C:/REPO/SellosDigitales-Des/src/java/WSCargaLlavesClient/lib" includes="log4j-1.2.17.jar"/>
        </jar>
    </target>
</project>

Any help to make my ANT script build a correct JAR file I'll appreciate

Jesús Ayala
  • 2,743
  • 3
  • 32
  • 48

1 Answers1

0

You need to have log4j jar under classpath i.e lib folder. If it is already there then classpath is not correctly set

Try just

  <attribute name="Class-Path" value="lib" />
M Sach
  • 33,416
  • 76
  • 221
  • 314
  • I just tried that but get same error, I have log4j-1.2.17.jar inside lib folder, and when I generate jar file, it is copied inside correctly, Manifest file says the following: Manifest-Version: 1.0 Ant-Version: Apache Ant 1.9.6 Created-By: 1.8.0_60-b27 (Oracle Corporation) Main-Class: mx.isban.csd.ws.Main Class-Path: lib – Jesús Ayala Feb 18 '16 at 18:05