Ant + Class-path Issue

后端 未结 2 631
轮回少年
轮回少年 2021-01-17 04:07

I have written an ANT script and finally am building the jar

here is the building of jar section


          


        
相关标签:
2条回答
  • 2021-01-17 04:39

    The reason for it not works was there not in the class path, the explanation is given here: Java-Jar-Ignores-Classpath-Workaround

    0 讨论(0)
  • 2021-01-17 04:44

    First of all the "Main-Class" and "Class-Path" manifiest entries is only used for executeable jars. In other words when you invoke java as follows:

    java -jar foo.jar
    

    Invoking java using the -cp option means you're supplying your own classpath and note that you also have to provide the main class on the command line as well.

    Secondly you need to replace the ":" characters with spaces:

    <attribute name="Class-Path" value=". lib/activation.jar lib/antlr-2.7.6.jar lib/asm-attrs.jar lib/asm.jar lib/cglib-2.1.3.jar lib/commons-collections-2.1.1.jar lib/commons-logging-1.1.jar lib/dom4j-1.6.1.jar lib/ehcache-1.2.3.jar lib/ejb3-persistence.jar lib/hibernate-annotations.jar lib/hibernate-commons-annotations.jar lib/hibernate-entitymanager.jar lib/hibernate-tools.jar lib/hibernate3.jar lib/javassist.jar lib/jdbc2_0-stdext.jar lib/jta.jar lib/mysql-connector-java-5.1.5-bin.jar"/>
    

    Finally I'd recommend using the manifestclasspath task to build your classpath string for you. It will correcly resolve any relative links between your jar and it's run-time dependencies.

    <manifestclasspath property="mf.classpath" jarfile="${destination}/@{name}.jar">
        <classpath>
            <fileset dir="lib" includes="*.jar"/>
        <classpath>
    </manifestclasspath>
    
    <jar jarfile="${destination}/@{name}.jar">
        <fileset dir="${output}">
           <include name="abc/xyz/@{name}/**"/>
        </fileset>
        <zipfileset dir="lib" prefix="lib/"/>
        <manifest>
           <attribute name="Main-Class" value="com.abc.xyz.HelloWorld"/>
           <attribute name="Class-Path" value=". ${mf.classpath}"/>
        </manifest>
    </jar>
    

    One final observation.... Why are you including the contents of the lib directory inside the jar? (The zipfileset tag in the jar command?)

    This appears unnecessary, all you need to do is ensure that the run-time dependencies are present in a lib directory as specified in your Class-Path manifest entry.

    0 讨论(0)
提交回复
热议问题