NoClassDefFoundError at runtime with ant

Well, I am trying to set up a project using Ant and this is what I get:

     Volume in drive D is WinMedia
     Volume Serial Number is 8ED9-B662

     Directory of D:\Dropbox\EclipseWorkspace\PIRS_Arturas_Masaitis\src\lib

    2012.11.20  16:11    <DIR>          .
    2012.11.20  16:11    <DIR>          ..
    2012.10.16  22:03           315.805 commons-lang3-3.1.jar
    2012.10.23  23:08           176.897 commons-validator-1.4.0.jar
    2012.11.20  15:30    <DIR>          hibernate
    2012.11.16  04:48           253.160 junit-4.10.jar
    2012.10.22  02:02           489.883 log4j-1.2.17.jar
    2012.10.31  23:00         1.581.066 mockito-all-1.9.5.jar
    2012.11.02  19:54           651.643 mybatis-3.1.1.jar
    2012.11.01  04:37           832.960 mysql-connector-java-5.1.22-bin.jar
                   7 File(s)      4.301.414 bytes
                   3 Dir(s)   7.277.907.968 bytes free

    D:\Dropbox\EclipseWorkspace\PIRS_Arturas_Masaitis\src\lib>cd ../..

    D:\Dropbox\EclipseWorkspace\PIRS_Arturas_Masaitis>ant run
    Buildfile: D:\Dropbox\EclipseWorkspace\PIRS_Arturas_Masaitis\build.xml


        [javac] Compiling 1 source file to D:\Dropbox\EclipseWorkspace\PIRS_Arturas_

          [jar] Building jar: D:\Dropbox\EclipseWorkspace\PIRS_Arturas_Masaitis\dist

         [java] Exception in thread "main" java.lang.NoClassDefFoundError: org/apach
         [java]     at com.nortal.pirs.userinterface.fakestarter.FakeUserInterface.<
    init>(Unknown Source)
         [java]     at com.nortal.pirs.userinterface.fakestarter.FakeUserInterface.m
    ain(Unknown Source)
         [java] Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger

         [java]     at$
         [java]     at$
         [java]     at Method)
         [java]     at
         [java]     at java.lang.ClassLoader.loadClass(
         [java]     at sun.misc.Launcher$AppClassLoader.loadClass(

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

    Total time: 2 seconds



Ok first you can see that I have log4j in src / lib folder and in the second part you can see that it was not found at runtime. Quite strange, because it compiles fine, it just can't find it at runtime.

My build.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project name="PIRS" default="dist" basedir=".">
    <description>PIRS Arturas Masaitis</description>
    <property name="src" location="src"/>
    <property name="build" location="build"/>
    <property name="dist" location="dist"/>
    <property name="lib.dir" location="src/lib"/>

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

    <target name="init">
        <mkdir dir="${build}"/>

    <target name="compile" depends="init">
        <javac includeantruntime="false" srcdir="${src}" destdir="${build}">
            <path refid="classpath"/>

    <target name="jar" depends="compile" description="generate the jar">
        <mkdir dir="${dist}/jar"/>
        <jar destfile="${dist}/jar/PIRS_Arturas_Masaitis.jar" basedir="${build}">
                <attribute name="Main-Class" value="com.nortal.pirs.userinterface.fakestarter.FakeUserInterface"/>

    <target name="clean" description="clean up" >
        <delete dir="${build}"/>
        <delete dir="${dist}"/>

    <target name="run" depends="jar">
        <java jar="${dist}/jar/PIRS_Arturas_Masaitis.jar" fork="true">
        <classpath refid="classpath"/>




The string doesn't actually change anything.

Well, any ideas on this? Thanks in advance.


source to share

1 answer

You runjava -jar ...

-classpath ...

into a common problem here: when you run something with a command , the property is discarded. ... The reason is security (Jar files can be digitally signed to ensure they haven't been modified, and it would be so easy to make java

another Jar load a dependency with the same content, but with the functionality captured).

The solution is simple: add the attribute Class-Path: ...

to your file MANIFEST.MF

as you did with Main-Class: ...




All Articles