开发者

Execute a jar file using Ant

开发者 https://www.devze.com 2023-01-04 00:25 出处:网络
I am trying to crea开发者_JAVA百科te a runnable jar file from java classes using ant. The java classes use external jars. When I execute the build.xml its showing class not found exception while runni

I am trying to crea开发者_JAVA百科te a runnable jar file from java classes using ant. The java classes use external jars. When I execute the build.xml its showing class not found exception while running the java program. Its compiling fine.

Part of My source code:

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

<path id="project-classpath">
   <fileset dir="C:/xmldecode/lib">   
      <include name="*.jar"/>
   </fileset> 
</path>

 <target name="compile" depends="prepare">               
    <javac srcdir="${src.dir}" destdir="${classes.dir}">    
       <classpath refid="project-classpath"/>
    </javac> 
 </target>

 <target name="jar" depends="compile">
    <copy todir="${classes.dir}">     
       <fileset dir="C:/xmldecode/lib"/>    
    </copy>

    <pathconvert property="mf.classpath" pathsep=";"> 
       <path refid="project-classpath" /> 
       <flattenmapper /> 
    </pathconvert> 

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


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


Your problem is that the manifest classpath entries are not separated by a ";" character. The following will work better I think:

<pathconvert property="mf.classpath" pathsep=" "> 
    <path refid="project-classpath" /> 
    <flattenmapper /> 
</pathconvert> 

Could I suggest using the new ANT task manifestclasspath ?

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

This powerful method will determine paths relative to the jar's location, for example if the jar's dependencies are located in a lib directory

0

精彩评论

暂无评论...
验证码 换一张
取 消