繁体   English   中英

Ant部署。 Jar 找不到主 class

[英]Ant deployment. Jar can't find the main class

创建简单的测试 java 项目只是为了了解如何使用 Ant 来部署应用程序。 Java 项目使用 Swing 创建 JFrame 和一个 JLabel 来表示“Hello World”。 它看起来像这样:

package com.mytest;
import javax.swing.*;        

public class home {
    private static void createAndShowGUI() {
        JFrame frame = new JFrame("HelloWorldSwing");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JLabel label = new JLabel("Hello World");
        frame.getContentPane().add(label);
        frame.pack();
        frame.setVisible(true);
    }
    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

然后我创建了 build.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<project default="mytest" basedir=".">
  <target name ="mytest" description="Create a jar for the Test project">
    <echo message="starting MyTest jar creation..." />
    <jar jarfile="mytest.jar" includes="*.class" basedir="bin">
        <manifest>
            <attribute name="Created-By" value="1.6.0_04 (Sun Microsystems Inc.)" />
            <attribute name="Built-By" value="Me" />
            <attribute name="Implementation-Version" value="1.0" />
            <attribute name="Main-Class" value="com.mytest.home" />
        </manifest>
    </jar>
    <echo message="MyTest jar created..." />
  </target>
</project>

问题是,一旦我通过 Eclipse 运行部署,就会创建 jar,但我无法启动它。 我收到消息:找不到主要的 class: com.mytest.home

怎么了? 这似乎是简单直接的过程。 我错过了什么吗?

谢谢。

我在您的 ant 脚本中找不到编译任务。 如果您没有编译任务,如何生成 class 文件?

也只需解压缩 jar 文件,看看 class 是否真的存在?

我对这一切有点困惑。 据我所知 Eclipse 正在运行中构建项目,当您键入时,当您决定部署时,您的部署脚本只需要 package 一切按照您在脚本中指定的方式。 它基本上将 class 文件从您的项目复制到 jar 文件中。

对于我的具体问题,原来我缺少<fileset>元素,它是<jar>元素的子元素,所以我的 xml 脚本现在看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<project default="mytest" basedir=".">
  <target name ="mytest" description="Create a jar for the Test project">
    <echo message="starting MyTest jar creation..." />
    <jar jarfile="mytest.jar" includes="*.class" basedir=".">
        <fileset dir="./bin" />
        <manifest>
            <attribute name="Created-By" value="..." />
            <attribute name="Built-By" value="Me" />
            <attribute name="Implementation-Version" value="1.0" />
            <attribute name="Main-Class" value="com.mytest.home" />
        </manifest>
    </jar>
    <echo message="MyTest jar created..." />
  </target>
</project>

我试图运行 jar 文件,它工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM