2013-10-02 2 views
2

Я разрабатываю проект java. Мой инструктор хочет, чтобы мы строго использовали apache ant для компиляции. Я ничего не знал об этом и не знаю об апаше. Я создал файл сборки ant и отредактировал его для целевой «run», чтобы программа запускалась после команды ant run.Ant: NoClassDefFound exception

Я получаю следующее сообщение об ошибке,

Exception in thread "main" java.lang.NoClassDefFoundError: src/Client 
    [java] Caused by: java.lang.ClassNotFoundException: src.Client 
    [java]  at java.net.URLClassLoader$1.run(URLClassLoader.java:217) 
    [java]  at java.security.AccessController.doPrivileged(Native Method) 
    [java]  at java.net.URLClassLoader.findClass(URLClassLoader.java:205) 
    [java]  at java.lang.ClassLoader.loadClass(ClassLoader.java:321) 
    [java]  at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294) 
    [java]  at java.lang.ClassLoader.loadClass(ClassLoader.java:266) 
    [java] Could not find the main class: src.Client. Program will exit. 

Я думаю, что я решил всю целевую зависимость. Но эта ошибка сохраняется.

Может кто-нибудь мне помочь? Это мой муравей build.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<!-- WARNING: Eclipse auto-generated file. 
       Any modifications will be overwritten. 
       To include a user specific buildfile here, simply create one in the same 
       directory with the processing instruction <?eclipse.ant.import?> 
       as the first entry and export the buildfile again. --> 
<project basedir="." default="build" name="client"> 
    <property environment="env"/> 
    <property name="ECLIPSE_HOME" value="../../../../usr/lib/eclipse"/> 
    <property name="debuglevel" value="source,lines,vars"/> 
    <property name="target" value="1.6"/> 
    <property name="source" value="1.6"/> 
    <path id="client.classpath"> 
     <pathelement location="bin"/> 
     <pathelement location="libthrift-1.0.0-javadoc.jar"/> 
     <pathelement location="libthrift-1.0.0.jar"/> 
     <pathelement location="log4j-1.2.14.jar"/> 
     <pathelement location="slf4j-api-1.5.8.jar"/> 
     <pathelement location="slf4j-log4j12-1.5.8.jar"/> 
    </path> 
    <target name="init"> 
     <mkdir dir="bin"/> 
    <mkdir dir="build"/> 
     <copy includeemptydirs="false" todir="bin"> 
      <fileset dir="src"> 
       <exclude name="**/*.java"/> 
      </fileset> 
     </copy> 
    </target> 
    <target name="clean"> 
     <delete dir="bin"/> 
    </target> 
    <target depends="clean" name="cleanall"/> 
    <target depends="build-subprojects,build-project" name="build"/> 
    <target name="build-subprojects"/> 
    <target depends="init" name="build-project"> 
     <echo message="${ant.project.name}: ${ant.file}"/> 
     <javac debug="true" debuglevel="${debuglevel}" destdir="bin" source="${source}" target="${target}"> 
      <src path="src"/> 
      <classpath refid="client.classpath"/> 
     </javac> 
    </target> 
    <target name="jar" depends="init"> 
     <mkdir dir="build/jar" /> 
     <jar destfile="client.jar" basedir="bin"> 
      <manifest> 
       <attribute name="Main-Class" value="src.Client" /> 
      </manifest> 
     </jar> 
    </target> 
    <target description="Build all projects which reference this project. Useful to propagate changes." name="build-refprojects"/> 
    <target description="copy Eclipse compiler jars to ant lib directory" name="init-eclipse-compiler"> 
     <copy todir="${ant.library.dir}"> 
      <fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar"/> 
     </copy> 
     <unzip dest="${ant.library.dir}"> 
      <patternset includes="jdtCompilerAdapter.jar"/> 
      <fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar"/> 
     </unzip> 
    </target> 
    <target description="compile project with Eclipse compiler" name="build-eclipse-compiler"> 
     <property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter"/> 
     <antcall target="build"/> 
    </target> 
    <target name="Client"> 
     <java classname="Client" failonerror="true" fork="yes"> 
      <classpath refid="client.classpath"/> 
     </java> 
    </target> 
    <target name="run" depends="jar"> 
     <java jar="client.jar" fork="true"> 
     </java> 
    </target> 
</project> 

Может кто-нибудь определить ошибку для меня?

+0

в client.classpath добавить путь Src также. и попробовать. – Zeus

ответ

2

Exception java.lang.NoClassDefFoundError возникает, когда какой-либо класс, ваш или в библиотеке, от которой зависит ваш код, попробуйте использовать символ, который еще не загружен. Погрузчик класса использует java-путь package_aa.package_bb.SomeClass как ключ в набор классов, который он знает, заданный -classpath или -cp. В вашем случае этот «репозиторий» - bin, libthrift-1.0.0-javadoc.jar, libthrift-1.0.0.jar, log4j-1.2.14.jar, slf4j-api-1.5.8.jar, slf4j-log4j12-1.5.8.jar.

В вашем контексте, когда класс p1.p2.Client будет загружен, загрузчик классов ищет bin/p1/p2/Client.class, ищет/p1/p2/Client.class в каждую банку и, когда все пути поиска исчерпаны, исключение

В этой части вашего build.xml:.

<target name="jar" depends="init"> 
    <mkdir dir="build/jar" /> 
    <jar destfile="client.jar" basedir="bin"> 
     <manifest> 
      <attribute name="Main-Class" value="src.Client" /> 
     </manifest> 
    </jar> 
</target> 

value="src.Client" является неправильным, оно должно быть логический путь java, то есть пакет + имя класса. src - это каталог, а не пакет. Если у вас нет пакета, это просто имя класса.

Я полагаю

<attribute name="Main-Class" value="Client" /> 

Это должно сделать трюк.

Ссылка:

EDIT после первой отладки появляется вторая ошибка:

<target name="run" depends="jar"> 
    <java jar="client.jar" fork="true"> 
    </java> 
</target> 

не может работать, потому что Client.jar не автономна, она требует много depndencies, которые Арен Здесь указано. Вы должны использовать classpath ref, определенный поверх вашего build.xml.

Это может быть писал:

<target name="run" depends="jar"> 
    <java classpathref="client.classpath" fork="true" classname="Client" /> 
</target> 
+0

Ummm .... Его не работает .... –

+0

Какое сообщение об ошибке? – Aubin

+0

Его то же самое ... –