2012-06-06 494 views
1

嗨,我想創建一個螞蟻生成,我可以從命令提示符運行。 當我運行包含main方法的jar文件我得到這個:編譯螞蟻建立和運行jar

Exception in thread "main" java.lang.NoClassDefFoundError: com/fmd/raptorurls/RaptorURLs 
    Caused by: java.lang.ClassNotFoundException: com.fmd.raptorurls.RaptorURLs 
      at java.net.URLClassLoader$1.run(Unknown Source) 
      at java.security.AccessController.doPrivileged(Native Method) 
      at java.net.URLClassLoader.findClass(Unknown Source) 
      at java.lang.ClassLoader.loadClass(Unknown Source) 
      at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) 
      at java.lang.ClassLoader.loadClass(Unknown Source) 
      at java.lang.ClassLoader.loadClassInternal(Unknown Source) 
    Could not find the main class: com.fmd.raptorurls.RaptorURLs. Program will exit. 

這裏是我的Ant構建:

<?xml version="1.0"?> 
    <project name="RaptorURLCheck" basedir="." default="cleanDistFiles"> 
    <tstamp> 
    <format property="currentYear" pattern="yyyy" /> 
    </tstamp> 

    <!-- =================================================================== --> 
    <!-- Global Properties - define in build.properties      --> 
    <!-- =================================================================== --> 
    <property file="build.properties" /> 

    <!-- =================================================================== --> 
    <!-- Paths                --> 
    <!-- =================================================================== --> 
    <path id="runtime-libs"> 
    <fileset dir="${lib.dir}"> 
     <include name="**/*.jar" /> 
    </fileset> 
    </path> 

    <path id="manifest-classpath"> 
    <fileset dir="${lib.dir}"> 
     <include name="**/*.jar" /> 
    </fileset> 
    </path> 

    <manifestclasspath property="jar.classpath" jarfile="${lib.dir}/${project.name}.jar" > 
    <classpath refid="manifest-classpath" /> 
    </manifestclasspath> 

    <target name="clean" > 
    <echo>***** Deleting files for ${project.name} *****</echo> 
    <delete failonerror="false" dir="${bin.dir}" /> 
    <delete failonerror="false" dir="${dist.dir}" /> 
    <delete failonerror="false" dir="${log.dir}" /> 
    <delete failonerror="false" dir="${conf.dir}" /> 
    </target> 

    <target name="init" depends="clean"> 
    <echo>***** Creating required directories for ${project.name} build *****</echo> 
    <tstamp /> 
    <mkdir dir="${bin.dir}" /> 
    <mkdir dir="${dist.dir}" /> 
    <mkdir dir="${log.dir}" /> 
    <mkdir dir="${conf.dir}" /> 
    </target> 

    <target name="compile" depends="init" > 
    <echo>***** Compiling source files for ${project.name} *****</echo> 
    <!--This javac compiles the java/src directory--> 
    <javac srcdir="${src.dir}" includeantruntime="false" destdir="${bin.dir}" debug="on" debuglevel="lines,var,source"> 
     <include name="**/*.java" /> 
     <classpath refid="runtime-libs" /> 
    </javac> 

    <copy todir="${bin.dir}"> 
     <fileset dir="${src.dir}"> 
     <include name="**/*.html" /> 
     <include name="**/*.css" /> 
     </fileset> 
     <fileset dir="${config.dir}"> 
     <include name="*-.xml" /> 
     <include name="*.xsd" /> 
     <include name="*.properties" /> 
     </fileset> 
    </copy> 
    </target> 

    <target name="jar" depends="compile" description="Packages app as jar" > 
    <echo>***** Creating jar distribution for ${project.name} *****</echo> 
    <jar destfile="${lib.dir}/${project.name}.jar"> 
     <manifest> 
     <attribute name="Class-Path" value="${jar.classpath}" /> 
     <attribute name="Built-By" value="${user.name}" /> 
     <attribute name="Main-Class" value="${main.class}" /> 
     </manifest> 
     <fileset dir="${bin.dir}"> 
     <include name="**/*.class*" /> 
     <include name="**/*.xml" /> 
     <include name="**/*.html" /> 
     <include name="**/*.css" /> 
     <include name="**/*.properties" /> 
     </fileset> 
     <fileset dir="${config.dir}"> 
     <include name="**/*.xml" /> 
     <include name="**/*.xsd" /> 
     </fileset> 
    </jar> 
    </target> 

    <target name="copyDistFiles" depends="jar"> 
    <echo>***** Copying distribution files for ${project.name} *****</echo> 
    <mkdir dir="${dist.dir.lib}" /> 
    <mkdir dir="${dist.dir.conf}" /> 
    <mkdir dir="${dist.dir.log}" /> 
    <copy todir="${dist.dir.lib}"> 
     <fileset dir="${lib.dir}" excludes="*.db" /> 
    </copy> 
    <copy todir="${dist.dir.conf}"> 
     <fileset dir="${conf.dir}" /> 
    </copy> 
    </target> 

    <target name="zip" depends="copyDistFiles"> 
    <echo>***** Zipping distribution files for ${project.name} *****</echo> 
    <zip destfile="./Linux_5.5_${project.name}.zip" basedir="${dist.dir}" excludes="*.db" /> 
    </target> 

    <target name="cleanDistFiles" depends="zip"> 
    <echo>***** Deleting distribution files for ${project.name} *****</echo> 
    <delete dir="${dist.dir}/lib" /> 
    <delete dir="${dist.dir.conf}" /> 
    <delete dir="${dist.dir}/log" /> 
    </target> 
</project> 

,這裏是我的構建屬性:

project.name=${ant.project.name} 
    app.dir=${basedir} 
    src.dir=${basedir}/src 
    lib.dir=${basedir}/lib 
    log.dir=${basedir}/logs 
    conf.dir=${basedir}/config 
    bin.dir=./bin 
    bin.dir.lib=${bin.dir}/lib 
    dist.dir=./dist 
    dist.dir.conf=${dist.dir}/config 
    dist.dir.lib=${dist.dir}/lib 
    dist.dir.log=${dist.dir}/logs 
    main.class=com.fmd.raptorurls.RaptorURLs 
    spring.config.dir=./lib 
    config.dir=./config/ 
    all.config.dir=./apps/fps/raptorurls/config 
    build_env=all 

唐不知道我做錯了什麼。 另外我使用彈簧

+0

這個類是否存在? –

+0

或者您的Manifest有問題嗎? http://docs.oracle.com/javase/tutorial/deployment/jar/manifestindex.html – Deepend

+1

確實存在哪個類? – Podge

回答

0

A NoClassDefFoundError(與ClassNotFoundException不同)可能表示a number of things。但是,一開始,我會猜測,當你執行你的主類時,一些依賴關係就會丟失。檢查你的運行時類路徑。你需要的所有庫都存在嗎?