2010-01-01 39 views
8

Tôi có tệp xây dựng bên dưới cho Ant và cố gắng sử dụng đích 'chạy' để thực thi chương trình.trợ giúp với tệp kiến ​​- classpath cho tác vụ Java

<property name="springjar" location="E:/Tools/spring-30/dist/" /> 
<property name="logjar"  location="E:/Tools/commons-logging-1.1.1/" /> 

<patternset id="jar.files"><include name="**/*.jar"/></patternset> 

<path id="springlearn.classpath"> 
    <fileset dir="${springjar}"><patternset refid="jar.files"/></fileset> 
    <fileset dir="${logjar}"><patternset refid="jar.files"/></fileset> 
</path> 


<target name="run" depends="dist" description="Execute the Java Program"> 
    <java dir ="." fork="true" jar="dist\app.jar" classpathref ="springlearn.classpath"> 
    </java> 
</target> 

Sử dụng classpathref cùng, tôi có thể biên dịch thành công & tạo jar, nhưng khi sử dụng thời gian mục tiêu, tôi nhận được báo lỗi dưới đây

java.lang.NoClassDefFoundError: org/springframework/core/io/Resource 
Caused by: java.lang.ClassNotFoundException: org.springframework.core.io.Resource 
    at java.net.URLClassLoader$1.run(URLClassLoader.java:200) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:307) 
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:289) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:252) 
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320) 

Exception in thread "main"

Bất kỳ trợ giúp nào được đánh giá cao. Cảm ơn

+0

mục tiêu "địa phương" ở đâu? – skaffman

Trả lời

7

Đây là tệp Ant build.xml chung chung hoạt động tốt cho tôi. Xem nếu nó có thể giúp bạn cũng như:

<?xml version="1.0" encoding="UTF-8"?> 
    <project name="ui" basedir="." default="package"> 

    <property name="version" value="1.6"/> 
    <property name="haltonfailure" value="no"/> 

    <property name="out" value="out"/> 

    <property name="production.src" value="src"/> 
    <property name="production.lib" value="lib"/> 
    <property name="production.resources" value="config"/> 
    <property name="production.classes" value="${out}/production/${ant.project.name}"/> 

    <property name="test.src" value="test"/> 
    <property name="test.lib" value="lib"/> 
    <property name="test.resources" value="config"/> 
    <property name="test.classes" value="${out}/test/${ant.project.name}"/> 

    <property name="exploded" value="out/exploded/${ant.project.name}"/> 
    <property name="exploded.classes" value="${exploded}/WEB-INF/classes"/> 
    <property name="exploded.lib" value="${exploded}/WEB-INF/lib"/> 

    <property name="reports.out" value="${out}/reports"/> 
    <property name="junit.out" value="${reports.out}/junit"/> 
    <property name="testng.out" value="${reports.out}/testng"/> 

    <path id="production.class.path"> 
     <pathelement location="${production.classes}"/> 
     <pathelement location="${production.resources}"/> 
     <fileset dir="${production.lib}"> 
      <include name="**/*.jar"/> 
      <exclude name="**/junit*.jar"/> 
      <exclude name="**/*test*.jar"/> 
     </fileset> 
    </path> 

    <path id="test.class.path"> 
     <path refid="production.class.path"/> 
     <pathelement location="${test.classes}"/> 
     <pathelement location="${test.resources}"/> 
     <fileset dir="${test.lib}"> 
      <include name="**/junit*.jar"/> 
      <include name="**/*test*.jar"/> 
     </fileset> 
    </path> 

    <path id="testng.class.path"> 
     <fileset dir="${test.lib}"> 
      <include name="**/testng*.jar"/> 
     </fileset> 
    </path> 

    <available file="${out}" property="outputExists"/> 

    <target name="clean" description="remove all generated artifacts" if="outputExists"> 
     <delete dir="${out}" includeEmptyDirs="true"/> 
     <delete dir="${reports.out}" includeEmptyDirs="true"/> 
    </target> 

    <target name="create" description="create the output directories" unless="outputExists"> 
     <mkdir dir="${production.classes}"/> 
     <mkdir dir="${test.classes}"/> 
     <mkdir dir="${junit.out}"/> 
     <mkdir dir="${testng.out}"/> 
     <mkdir dir="${exploded.classes}"/> 
     <mkdir dir="${exploded.lib}"/> 
     <mkdir dir="${reports.out}"/> 
    </target> 

    <target name="compile" description="compile all .java source files" depends="create"> 
     <!-- Debug output 
       <property name="production.class.path" refid="production.class.path"/> 
       <echo message="${production.class.path}"/> 
     --> 
     <javac srcdir="src" destdir="${out}/production/${ant.project.name}" debug="on" source="${version}"> 
      <classpath refid="production.class.path"/> 
      <include name="**/*.java"/> 
      <exclude name="**/*Test.java"/> 
     </javac> 
     <javac srcdir="${test.src}" destdir="${out}/test/${ant.project.name}" debug="on" source="${version}"> 
      <classpath refid="test.class.path"/> 
      <include name="**/*Test.java"/> 
     </javac> 
    </target> 

    <target name="junit-test" description="run all junit tests" depends="compile"> 
     <!-- Debug output 
       <property name="test.class.path" refid="test.class.path"/> 
       <echo message="${test.class.path}"/> 
     --> 
     <junit printsummary="yes" haltonfailure="${haltonfailure}"> 
      <classpath refid="test.class.path"/> 
      <formatter type="xml"/> 
      <batchtest fork="yes" todir="${junit.out}"> 
       <fileset dir="${test.src}"> 
        <include name="**/*Test.java"/> 
       </fileset> 
      </batchtest> 
     </junit> 
     <junitreport todir="${junit.out}"> 
      <fileset dir="${junit.out}"> 
       <include name="TEST-*.xml"/> 
      </fileset> 
      <report todir="${junit.out}" format="frames"/> 
     </junitreport> 
    </target> 

    <taskdef resource="testngtasks" classpathref="testng.class.path"/> 
    <target name="testng-test" description="run all testng tests" depends="compile"> 
     <!-- Debug output 
       <property name="test.class.path" refid="test.class.path"/> 
       <echo message="${test.class.path}"/> 
     --> 
     <testng classpathref="test.class.path" outputDir="${testng.out}" haltOnFailure="${haltonfailure}" verbose="2"> 
      <classfileset dir="${out}/test/${ant.project.name}" includes="**/*.class"/> 
     </testng> 
    </target> 

    <target name="exploded" description="create exploded deployment" depends="testng-test"> 
     <copy todir="${exploded.classes}"> 
      <fileset dir="${production.classes}"/> 
     </copy> 
     <copy todir="${exploded.lib}"> 
      <fileset dir="${production.lib}"/> 
     </copy> 
    </target> 

    <target name="package" description="create package file" depends="exploded"> 
     <jar destfile="${out}/${ant.project.name}.jar" basedir="${production.classes}" includes="**/*.class"/> 
    </target> 

</project> 
+0

Tôi không thấy mục tiêu java ở đây. Vì vậy, tôi không nghĩ rằng điều này trả lời các câu hỏi. – Jimmy

+0

Nó hiển thị cách đặt đúng đường dẫn lớp trong Ant. Nó được dự định như một ví dụ mà OP có thể đã sử dụng khi họ hỏi câu hỏi này gần 6 năm trước. Tìm cách sử dụng tốt hơn cho thời gian của bạn so với các nhận xét như thế này. – duffymo

9

Từ java task docs:

Khi sử dụng thuộc tính bình, tất cả các thiết lập classpath được bỏ qua theo Sun's specification.

Vì vậy, đường dẫn bạn thiết lập không được xem xét chút nào.

Bạn có thể thêm dist/app.jar vào classpath của bạn và gọi những lớp học chính trực tiếp:

<java dir ="." fork="true" classname="com.yourdomain.YourMainClass"> 
    <classpath> 
    <path refid="springlearn.classpath" /> 
    <pathelement location="dist\app.jar" /> 
    </classpath> 
</java> 

Nếu bạn muốn gọi jar, bạn sẽ cần phải thiết lập một mục classpath trong biểu hiện khi bạn xây dựng nó . Hãy xem nhiệm vụ pathconvert, nó có thể hữu ích.

+0

Cảm ơn bạn! Tôi bắt đầu tomcat thông qua kiến ​​và bootstrap.jar của tomcat không có tomcat-juli.jar trong đó là classpath Manifest. Vì vậy, tôi đã thử sử dụng một classpath khác. Điều này không hoạt động nếu bạn sử dụng "jar" attritbute. Bây giờ tôi định nghĩa classname để tự chạy và cung cấp một classpath. Cảm ơn một lần nữa! – s3b

Các vấn đề liên quan