NoClassDefFoundError 在运行时与 ant [英] NoClassDefFoundError at runtime with ant

查看:29
本文介绍了NoClassDefFoundError 在运行时与 ant的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我正在尝试使用 Ant 配置项目,这就是我得到的:

well I'm trying to configure the project with Ant and this is what I get:

     D:\Dropbox\EclipseWorkspace\PIRS\src\lib>dir
     Volume in drive D is WinMedia
     Volume Serial Number is 8ED9-B662
    
     Directory of D:\Dropbox\EclipseWorkspace\PIRS\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\src\lib>cd ../..
    
    D:\Dropbox\EclipseWorkspace\PIRS>
    D:\Dropbox\EclipseWorkspace\PIRS>
    D:\Dropbox\EclipseWorkspace\PIRS>ant run
    Buildfile: D:\Dropbox\EclipseWorkspace\PIRS\build.xml
    
    init:
    
    compile:
        [javac] Compiling 1 source file to D:\Dropbox\EclipseWorkspace\PIRS\build
    
    jar:
          [jar] Building jar: D:\Dropbox\EclipseWorkspace\PIRS\dist
    \jar\PIRS.jar
    
    run:
         [java] Exception in thread "main" java.lang.NoClassDefFoundError: org/apach
    e/log4j/Logger
         [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.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]     ... 2 more
         [java] Java Result: 1
    
    BUILD SUCCESSFUL
    Total time: 2 seconds

D:\Dropbox\EclipseWorkspace\PIRS>

首先你可以看到我在文件夹 src/lib 中有 log4j,在第二部分你可以看到它在运行时没有找到.很奇怪,因为它编译得很好,它似乎在运行时找不到.

Well first you can see I have the log4j in the folder src/lib and in the second part you can see that it's not found at runtime. Pretty strange, because it compiles fine, it just seems to not be able to find that at runtime.

我的 build.xml :

My build.xml :

<?xml version="1.0" encoding="UTF-8"?>
<project name="PIRS" default="dist" basedir=".">
    <description>PIRS</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"/>
    </path>
    
    <target name="init">
        <mkdir dir="${build}"/>
    </target>
    
    <target name="compile" depends="init">
        <javac includeantruntime="false" srcdir="${src}" destdir="${build}">
        <classpath>
            <path refid="classpath"/>
        </classpath>
        </javac>
    </target>
    
    <target name="jar" depends="compile" description="generate the jar">
        <mkdir dir="${dist}/jar"/>
        <jar destfile="${dist}/jar/PIRS.jar" basedir="${build}">
            <manifest>
                <attribute name="Main-Class" value="com.nortal.pirs.userinterface.fakestarter.FakeUserInterface"/>
            </manifest>
        </jar>
   </target>
   
    <target name="clean" description="clean up" >
        <delete dir="${build}"/>
        <delete dir="${dist}"/>
    </target>
    
    <target name="run" depends="jar">
        <java jar="${dist}/jar/PIRS.jar" fork="true">
        <classpath refid="classpath"/>
            
        </java>
    </target>
        
</project>

实际上这条线似乎没有改变任何东西.

Actually the line doesn't seem to change anything.

嗯,有什么想法吗?提前致谢.

Well, any ideas on it? Thanks in advance.

推荐答案

您在这里遇到了一个常见问题:当您使用 java -jar ... 命令运行某些程序时,-classpath ... 属性被删除. 原因是安全性(可以对 Jar 文件进行数字签名以确保它们不被修改,并且制作 java 加载具有相同内容但具有劫持功能的不同依赖 Jar 文件).

You met a common issue here: when you run something with the java -jar ... command, the -classpath ... property is dropped. The reason is security (Jar files can be digitally signed to ensure they weren't modified and it would be so easy to make java load a different dependency Jar file with the same content but with hijacked functionality).

解决方案很简单:在您的 MANIFEST.MF 文件中也包含 Class-Path: ... 属性,就像您对 Main-Class 所做的一样:....

The solution is simple: include the Class-Path: ... attribute too in your MANIFEST.MF file as you did with Main-Class: ....

这篇关于NoClassDefFoundError 在运行时与 ant的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆