开发者

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 project uses Swing to create JFrame and one JLabel to say "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:

<?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"开发者_StackOverflow />
            <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. I get message: Could not find the main 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 . How are the class files getting generated if you do not have a compile task ?

Also just unzip the jar file and see if the class actually exists ?


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. It basically copies class files from your project into jar file.

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:

<?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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜