简体   繁体   中英

Building eclipse project with ANT - can't find main class

Building eclipse project with ant - Can't find main class

I'm trying to build my eclipse project into a .jar using ANT.

My directory structure is:

/

|---src/DynamDNS/.java files and build.xml

| ---bin/DynamDNS/.class files from eclipse

| ---lib/ .jars included in class path

Here's my build.xml:

<?xml version="1.0" ?>
<project name="TestMain" default="CreateJar">
  <target name="CreateJar" description="Create Jar file">
    <jar jarfile="DynamDNS.jar" basedir="." includes="**/.class">
        <manifest>
            <attribute name="Main-Class" value="DynamDNS.Connector" />
        </manifest>
    </jar>  
  </target>
</project>

The thing builds correctly, and generates a jar. However when I run the jar with: java -jar DynamDNS.jar

I get the error: Error: Could not find or load main class DynamDNS.Connector

I've definitely spelled my classnames correctly.

Also, the classes seem to be in the jar:

jar tvf DynamDNS.jar 
     0 Tue Apr 21 17:11:26 IST 2015 META-INF/
   135 Tue Apr 21 17:11:24 IST 2015 META-INF/MANIFEST.MF
  5947 Mon Apr 20 21:07:58 IST 2015 Connector.class
   979 Mon Apr 20 20:40:44 IST 2015 IPV4.class
   370 Mon Apr 20 20:40:44 IST 2015 InvalidIPV4Exception.class

The package name is DynamDNS

Has anyone any ideas?

If your main-class is DynamDNS.Connector, it means the Connector class is in the package DynamDNS, and hence should be in a DynamDNS folder inside the jar, whereas your looks to be at the top level of the jar.

I think the issue is with your jar task in your ant file. Drop the includes and use your bin folder as the basedir:

<jar jarfile="DynamDNS.jar" basedir="bin">
    <manifest>
        <attribute name="Main-Class" value="DynamDNS.Connector" />
    </manifest>
</jar>  

(See https://ant.apache.org/manual/Tasks/jar.html for examples)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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