0

I have created a executable jar file using build.

<target name="deploy" depends="compile">
<jar jarfile="${deploy.home}/${app.name}.jar" basedir="${build.home}">
<manifest>
<attribute name="Main-Class" value="abc.xyz"/>
</manifest>
</jar>

Here the xyz call has the main method to execute the whole program..

public static void main(String[] args) {
        File buildFile = new File("build.xml");
        Project p = new Project();
        p.setUserProperty("ant.file", buildFile.getAbsolutePath());
        p.init();
        ProjectHelper helper = ProjectHelper.getProjectHelper();
        p.addReference("ant.projectHelper", helper);
        helper.parse(p, buildFile);
        p.executeTarget(p.getDefaultTarget());
} 

The jar file is being created successfuly. When I execute the like java -jar appname.jar, it is throwing exception --

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/tools/ant/Project
        at abc.xyz.main(xyz.java:20)
Caused by: java.lang.ClassNotFoundException: org.apache.tools.ant.Project
        at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)

The project class not found. Its there in ant.jar.

But when i execute the abc.java class simply by on command prompt java abc its working fine.

Pleased help me to get the cause? and its solution.

4

1 回答 1

0

将需要的库添加到 jar 的类路径中

<target name="deploy" depends="compile">
<jar jarfile="${deploy.home}/${app.name}.jar" basedir="${build.home}">
<manifest>
<attribute name="Main-Class" value="abc.xyz"/>
<attribute name="Class-Path" value="ant.jar"/>
</manifest>
</jar>

如果您需要一个以上的罐子,则必须用空格将它们分开。

于 2013-09-05T12:01:32.363 回答