简体   繁体   English

Ant部署。 Jar 找不到主 class

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

Created simple test java project just to learn how to use Ant to deploy an application.创建简单的测试 java 项目只是为了了解如何使用 Ant 来部署应用程序。 Java project uses Swing to create JFrame and one JLabel to say "Hello World". Java 项目使用 Swing 创建 JFrame 和一个 JLabel 来表示“Hello World”。 It looks like this:它看起来像这样:

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();
            }
        });
    }
}

Then I created build.xml file:然后我创建了 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>

The Problem is, once I run deployment through Eclipse, jar is created, but I can't start it.问题是,一旦我通过 Eclipse 运行部署,就会创建 jar,但我无法启动它。 I get message: Could not find the main class: com.mytest.home ?我收到消息:找不到主要的 class: com.mytest.home

What is wrong?怎么了? It seems like simple straight forward process.这似乎是简单直接的过程。 Am I missing something?我错过了什么吗?

Thanks.谢谢。

I can't find the compile task in your ant script.我在您的 ant 脚本中找不到编译任务。 How are the class files getting generated if you do not have a compile task?如果您没有编译任务,如何生成 class 文件?

Also just unzip the jar file and see if the class actually exists?也只需解压缩 jar 文件,看看 class 是否真的存在?

I am little bit baffled by all of this.我对这一切有点困惑。 As far as I know Eclipse is building project on the run, as you type, then when you decide to deploy, your deployment script just need to package everything the way you specified in the script.据我所知 Eclipse 正在运行中构建项目,当您键入时,当您决定部署时,您的部署脚本只需要 package 一切按照您在脚本中指定的方式。 It basically copies class files from your project into jar file.它基本上将 class 文件从您的项目复制到 jar 文件中。

For my specific problem, it turned out I was missing < fileset > element, which is child of < jar > element, so my xml script now is looking like this:对于我的具体问题,原来我缺少<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>

I tried to run jar file, and it worked.我试图运行 jar 文件,它工作。

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

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