java - 使用 ant 和 genJar 创建可运行的 jar

标签 java ant jar executable-jar

我编写了以下 ant 目标:

<target name="approval_request_parser_compile">
        <taskdef resource="genjar.properties" classpath="GenJar.jar"/>
        <genjar jarfile="prod/lib/approvalRequestParser.jar">

            <class>
                <fileset dir=".">
                 <include name="com.bet.Check.class"/>
                    <include name="com.bet.Approve.class"/>
                    <include name="com.bet.CheckText.class"/>
                    <include name="com.bet.Request.class"/>
                    <include name="com.bet.ApprovalRequestParser.class"/> #contains the main method
                </fileset>
            </class>
            <classpath>
                <fileset dir="lib">
                    <include name="*.jar" />
                </fileset>
                <pathelement path="."/>
            </classpath>
        </genjar>
    </target>

jar 已成功创建,但是当我转到/prod/lib 并输入:

java -jar approvalRequestParser.jar

引发此错误:

no main manifest attribute, in approvalRequestParser.jar

有什么想法吗?

最佳答案

我没有使用 genjar,但您需要创建 manifest 文件,请参阅下面的示例

<target name="buildJAR" depends="compile">
  <tstamp>
    <format property="BUILD.TSTAMP" pattern="MMMM dd yyyy hh:mm:ss" locale="en"/>
    </tstamp>
    <buildnumber file="build.number"/>
  <mkdir dir="classes/META-INF"/>
  <echo file="classes/META-INF/version.txt">Version: ${VERSION} (build #: ${build.number}, ${BUILD.TSTAMP})</echo>
    <manifest file="classes/META-INF/MANIFEST.MF">
        <attribute name="Specification-Title" value="ANY TEXT CAN GO HERE"/>
        <attribute name="Specification-Version" value="${VERSION}"/>
        <attribute name="Specification-Vendor" value="COMPANY NAME"/>
        <attribute name="Implementation-Title" value="WHAT CODE DOES"/>
        <attribute name="Implementation-Version" value="${VERSION} (build #: ${build.number}, ${BUILD.TSTAMP})"/> 
        <attribute name="Implementation-Vendor" value="WHO DOES IT"/>
    </manifest>
  <jar destfile="JAR FILE" basedir="classes" 
       includes="${CORE_CODE}" manifest="classes/META-INF/MANIFEST.MF"/>
</target>

我认为您可以对 genjar 任务应用相同的方法

关于java - 使用 ant 和 genJar 创建可运行的 jar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32494860/

相关文章:

java - 云应用程序使用多个架构还是单个架构?

java - 为什么将自动装箱标记为警告?

java - 执行由 Jasmin 生成的文件 .classes 时出错

java - 程序在 IDE 中运行,但不是作为 .jar 文件运行

java - 编译并构建 asmack 的 jar

java - Java 中的运行时类型识别

java - 如何指定特定的 JAXB 实现?

java - 使用ant时出现NoClassDefFoundError

ant - 如何获取当前JUnitCore的访问权限来添加监听器?

ant - 在 Ant 中,如何动态构建引用属性文件的属性?