Apache Ant java.lang.NoClassDefFoundError

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17861699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 17:50:31  来源:igfitidea点击:

Apache Ant java.lang.NoClassDefFoundError

javaant

提问by user2619249

Possible Answer Stack Over flow question

可能的答案堆栈溢出问题

My problem is same as described in the above thread.I went throught the solution and its not working. ant compileworking perfectly. ant jarsaying cant load the main class

我的问题与上述线程中描述的相同。我解决了该解决方案但它不起作用。 蚂蚁编译工作完美。ant jar说无法加载主类

Exception in thread "main" java.lang.NoClassDefFoundError: LoadServer (wrong name: org/module/loader/LoadServer)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
    at java.net.URLClassLoader.access0(URLClassLoader.java:71)
    at java.net.URLClassLoader.run(URLClassLoader.java:361)
    at java.net.URLClassLoader.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)

I did the following in commnad line

我在命令行中执行了以下操作

JAVA_HOME="/usr/local/java/jdk1.7.0_21"
ANT_HOME="/usr/share/ant/"
PATH="$ANT_HOME/bin:$PATH"

Morever if I start a new terminal and type echo$JAVA_HOMEits showing the above result but echo$ANT_HOMEis giving null.

此外,如果我启动一个新终端并输入echo$JAVA_HOME它显示上述结果但echo$ANT_HOME给出空值。

here is a screen shot

这是一个屏幕截图

Initial Error

初始错误

     <project name="Raxa-4" basedir="." default="main">

    <property name="src.dir"      value="src"/>
    <property name="prop.dir"     value="properties"/>
    <property name="resource.dir" value="resource"/>
    <property name="lib.dir"      value="lib"/>
    <property name="build.dir"    value="build"/>
    <property name="classes.dir"  value="${build.dir}/classes"/>
    <property name="jar.dir"      value="${build.dir}/jar"/>
    <property name="main-class"   value="org.raxa.module.loader.LoadServer"/>

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

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

    <target name="compile">
        <mkdir dir="${classes.dir}"/>
        <javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath" includeantruntime="false" />
        <copy todir="${classes.dir}">
            <fileset dir="${resource.dir}" excludes="**/*.java"/>
            <fileset dir="${prop.dir}" excludes="**/*.java"/>
        </copy>
    </target>

   <target name="jar" depends="compile">
        <mkdir dir="${jar.dir}"/>
         <classpath>
            <path refid="classpath"/>
            <path location="${jar.dir}/${ant.project.name}.jar"/>
        </classpath>
        </jar>
    </target>


    <target name="run" depends="jar">
        <java jar="${jar.dir}/${ant.project.name}.jar" fork="true"/>

    </target>

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

    <target name="main" depends="clean,run"/>

</project>

Edited:

编辑:

    <property name="src.dir"      value="src"/>
    <property name="prop.dir"     value="properties"/>
    <property name="resource.dir" value="resource"/>
    <property name="lib.dir"      value="lib"/>
    <property name="build.dir"    value="build"/>
    <property name="classes.dir"  value="${build.dir}/classes"/>
    <property name="jar.dir"      value="${build.dir}/jar"/>
    <property name="main-class"   value="org.raxa.module.loader.LoadServer"/>

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

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

    <target name="compile">
        <mkdir dir="${classes.dir}"/>
        <javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath" includeantruntime="false" />
        <copy todir="${classes.dir}">
            <fileset dir="${resource.dir}" excludes="**/*.java"/>
            <fileset dir="${prop.dir}" excludes="**/*.java"/>
        </copy>
    </target>

   <target name="jar" depends="compile">
        <mkdir dir="${jar.dir}"/>
        <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
            <manifest>
                <attribute name="Main-Class" value="${main-class}"/>
            </manifest>
        </jar>
    </target>


    <target name="run" depends="jar">
        <java jar="${jar.dir}/${ant.project.name}.jar" fork="true"/>
        <classpath>
            <path refid="classpath"/>
            <path location="${jar.dir}/${ant.project.name}.jar"/>
        </classpath>
    </target>

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

    <target name="main" depends="clean,run"/>

</project>

NEW ERRORon ant run

蚂蚁运行的新错误

run:

跑:

[java] Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger
 [java]     at org.raxa.module.scheduler.TimeSetter.<clinit>(Unknown Source)
 [java]     at org.raxa.module.loader.LoadServer.main(Unknown Source)
 [java] Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger
 [java]     at java.net.URLClassLoader.run(URLClassLoader.java:366)
 [java]     at java.net.URLClassLoader.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 FAILED
/home/atul/Documents/workspace2/Raxa-4/build.xml:41: Problem: failed to create task or type classpath
Cause: The name is undefined.
Action: Check the spelling.
Action: Check that any custom tasks/types have been declared.
Action: Check that any <presetdef>/<macrodef> declarations have taken pla

ce.

ce。

采纳答案by fGo

Maybe the problem is that your classpath gets overriden. Try this one:

也许问题是您的类路径被覆盖了。试试这个:

<java classname="${main-class}" fork="true" >
  <classpath>
    <path refid="classpath"/>
    <pathelement location="${jar.dir}/${ant.project.name}.jar"/>
  </classpath>
</classpath> </java>

回答by David W.

You don't need ANT_HOMEdefined because the shell script antwill set that for you. However, if you did EXPORT ANT_HOME, you'll see it when you start a new terminal. Until you export an environment variable, it won't show up in child processes.

您不需要ANT_HOME定义,因为 shell 脚本ant会为您设置。但是,如果您这样做了EXPORT ANT_HOME,您会在启动新终端时看到它。在您导出环境变量之前,它不会出现在子进程中。

I noticed that the Ant class isn't showing up in the call. You should have seen some call to org.apache.tools.antsomewhere in the stack trace if this was an Ant issue. Can you run any other build task? Try:

我注意到 Ant 类没有出现在调用中。org.apache.tools.ant如果这是 Ant 问题,您应该已经看到对堆栈跟踪中某处的一些调用。您可以运行任何其他构建任务吗?尝试:

$ ant clean

Does that work?

那样有用吗?

Ant has extensive debugging trace which shows you way more information than you want to know. Try running antwith the -dparameter:

Ant 具有广泛的调试跟踪,可以向您显示比您想知道的更多的信息。尝试ant使用-d参数运行:

$ ant -d compile | tee ant.out

This will produce a lot of output which is why you need to pipe this into a file. Look at ant.outand see where it's failing. Did it call the compiletask?

这将产生大量输出,这就是为什么您需要将其通过管道传输到文件中的原因。看看它在ant.out哪里失败了。它调用了compile任务吗?

The ant-launcher calls ant.jarwhich then calls org.ant.tools.taskdefs.javac(If I remember the path) which then does a system command to call the javacexecutable directly. One of the things the dump will show is the exact command being executed when the compilation is done.

ant-launcher 调用ant.jar然后调用org.ant.tools.taskdefs.javac(如果我记得路径)然后执行系统命令来javac直接调用可执行文件。转储将显示的一件事是编译完成时正在执行的确切命令。

This will probably help you find where the error is happening. It could be that javacitself isn't executing, or that it is missing. I've seen this before where someone accidentally pointed JAVA_HOMEto a JRE and not a JDK. I've also seen this where the executable was 64bits, but the OS was 32bits.

这可能会帮助您找到错误发生的位置。可能是它javac本身没有执行,或者它丢失了。我之前见过有人不小心指向JAVA_HOMEJRE 而不是 JDK。我也看到过这个,可执行文件是 64 位,但操作系统是 32 位。

If the dump shows you the actual Java command being executed, see if you can copy it, and paste it into a shell script and execute the javaccommand directly. This way, we can see if the issue is with javacor with Ant. If you can execute javacwithout Ant, it's an Ant issue. Otherwise, it's an issue with your Java installation.

如果转储显示正在执行的实际 Java 命令,请查看是否可以复制它,并将其粘贴到 shell 脚本中并javac直接执行命令。通过这种方式,我们可以查看问题是javac与 Ant 相关还是与 Ant 相关。如果你可以在javac没有 Ant 的情况下执行,这是一个 Ant 问题。否则,这是 Java 安装的问题。