简体   繁体   中英

Apache Ant java.lang.NoClassDefFoundError

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 compile working perfectly. ant jar saying cant load the main class

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.access$100(URLClassLoader.java:71)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.net.URLClassLoader$1.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_HOME its showing the above result but echo$ANT_HOME is giving null.

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 ERROR on 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$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 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

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>

You don't need ANT_HOME defined because the shell script ant will 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.

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

$ ant clean

Does that work?

Ant has extensive debugging trace which shows you way more information than you want to know. Try running ant with the -d parameter:

$ 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.out and see where it's failing. Did it call the compile task?

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

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

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 javac command directly. This way, we can see if the issue is with javac or with Ant. If you can execute javac without Ant, it's an Ant issue. Otherwise, it's an issue with your Java installation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM