tags:

views:

54

answers:

2

I am attempting to create a JAR based on two separate Java packages. I can compile and run within Eclipse, but cannot get the code to function from the command line. I have Ant and the JDK correctly configured for usage, as I have an almost working Ant build script. The only problem is that the resulting JAR throws a ClassNotFoundException when I attempt to execute it.

The archive contains all the .class files from both packages in the correct directory hierarchy. Regardless, the JAR will throw the above mentioned exception.

The idea is to run this script from the top level directory that contains both packages.

Here are the relevant lines from my build script:

    <manifest file="MANIFEST.MF">
        <attribute name="Built-By" value="XBigTK13X"/>
        <attribute name="Main-Class" value="com.main.MainClass"/>
        <attribute name="Class-Path" value="./com/main/ ./secondpackage/shapes/" />
    </manifest>
    <jar destfile="App.jar"
       basedir="./bin"
       includes="**/*.class"
       manifest="MANIFEST.MF"
       excludes="App.jar"
    />
A: 

Look in the resulting JAR file to make sure that the two packages have the correct path from the root. Your Class-Path statement in the manifest may not match the structure of folders containing the .class files.

Verify it by opening the JAR with a zip util.

Kelly French
They are indeed located in the correct directory structure.
XBigTK13X
Which package is loading properly, main or secondpackage? I'm wondering if the separator for your Class-Path needs to be a semicolon: <attribute name="Class-Path" value="./com/main/;./secondpackage/shapes/" />
Kelly French
Changing the space separator to a semi-colon did not resolve the issue. Here is the error message generated when attempting to run the JAR: Caused by: java.lang.ClassNotFoundException: MainClass.jar
XBigTK13X
Can you add the full directory hierarchy for both packages to the question?
Kelly French
A: 

The JAR was correct the whole time. This error was thrown because I was attempting to run the JAR with the following command after creating a JAR:

java MainClass

I now realize that I need to explicitly target the JAR by using the following command:

java -jar MainClass.jar
XBigTK13X