ant run task:[java] java.lang.NoClassDefFoundError:org / apache / log4j / Logger

时间:2013-02-15 08:33:26

标签: ant jar runtime

我尝试从ant运行我的应用程序,该应用程序正在使用log4j jar 我现在有这个蚂蚁任务:

<property name="main-class"  value="com.controller.core.Controller"/>
<target name="dist"  
        description="generate the distribution" >
    <jar jarfile="${dist}/lib/controller.jar" basedir="${build}">
        <manifest>
            <attribute name="Main-Class" value="${main-class}"/>
        </manifest>
    </jar>
</target>
     <path id="class.path">
          <fileset dir="..\lib">
            <include name="**/*.jar" />
          </fileset>      
      </path>
     <target name="run">
            <java jar="${dist}/lib/controller.jar" fork="true" >         
            <classpath refid="class.path" />
            <classpath>
                <path location="../lib/misc"/>
            </classpath>
           </java>      
      </target>

或者我甚至给了它完整的路径,但仍然是同样的错误

<target name="run">
        <java jar="${dist}/lib/controller.jar" fork="true" >         
            <classpath>
                <path refid="test.classpath"/>
                <path location="d:\dev\svn\trunk\lib\misc\log4j.jar"/>
            </classpath>     
        </java>
</target>

其中log4j位于../lib/misc路径中。 但我仍然得到这个错误:

run:
     [java] java.lang.NoClassDefFoundError: org/apache/log4j/Logger
     [java]     at com.controller.core.Controller.<clinit>(Unknown Source)
     [java] Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger
     [java]     at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
     [java]     at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
     [java]     at java.security.AccessController.doPrivileged(Native Method)
     [java]     at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
     [java]     at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
     [java]     at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
     [java]     at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
     [java]     ... 1 more
     [java] Exception in thread "main"
     [java] Java Result: 1

现在当我用-v标志运行ant时,它给了我这个消息:

[java] When using 'jar' attribute classpath-settings are ignored. See the manual for more information.
    [java] Executing 'c:\Program Files (x86)\Java\jdk1.7.0_05\jre\bin\java.exe' with arguments:
    [java] '-classpath'
    [java] 'd:\dev\svn\trunk\lib\misc\log4j.jar'
    [java] '-jar'
    [java] 'D:\dev\svn\trunk\controller\dist\lib\controller.jar'

我在这里想念什么?

1 个答案:

答案 0 :(得分:5)

正如它所说:当使用jar属性时,忽略提供的类路径,因为jar文件会覆盖它。 ant手册链接到java文档,其中说:

  

当您使用此选项[-jar]时,JAR文件是所有用户类的源,并忽略其他用户类路径设置。

相反,在类路径中包含jar并使用classname属性。例如,

<java classname="${main-class}" fork="true" >         
  <classpath>
    <path refid="class.path" />
    <pathelement location="../lib/misc"/>
    <pathelement location="${dist}/lib/controller.jar"/>
  </classpath>
</java>