what is correct internal structure of JAR file

DaveM picture DaveM · Apr 2, 2012 · Viewed 16.3k times · Source

This is a really silly question that I can't fine a difinitive answer to.

Background.

I'm using Eclipse (with one of the ANT plugins, on an XP terminal).

I have just started playing with ANT, in the [jar] directive I am setting the location of my finished JAR file and I get the following when I 'unzip' the file

META-INF/MANIFEST.MF MyMainFile.class

which is consistent with that found on the oracle web site for the internal structure. (here http://docs.oracle.com/javase/tutorial/deployment/jar/view.html )

But when I try to run my file I get a 'main class not found' error ?

I have seen some other posts where people have 'unzipped' the JAR file and ended up with a structure of the following...

META-INF/MANIFEST.MF dtvcontrol/DTVControlApp.class

(from here http://www.coderanch.com/t/528312/java/java/standalone-application)

So should I get a structure where my class files are in a directory that reflects the name of the package... eg

META-INF/MANIFEST.MF MyPackage/MyMainFile.class

and if so, why am I getting the incorrect structure using ANT, or why are there 2 different 'correct' internal structures? (how to specifify main-class and classpath for each / control what I get)

Also in case you are interested, in the MANIFEST file states (build using ANT) [attribute name="Main-Class" value="MyPackage.MyMainFile"/]

Also the directory structure of the package under development is as follows...

/JavaDev/MyTestPackage/src (contains the source files) //JavaDev/MyTestPackage/bin (contains the class files from eclipse, or from the ANT JAVAC task, have I named it incorrectly? should I have called it build ? )

Further to this, when I create the jar I am not naming it 'MyTestPackage.jar' but simply 'test.jar' could this be causing a problem? I assume not as if I have well understood that is what the [main-class] definition stuff is all about.

Further to all this...

'MyTestPackage' is actualy a small visual error messaging library that I use elsewhere, and I have a second file that has a main class to use for testing. As such it points to various libraries (do I need to copy all the SWT libraries to a specified directory?)

I have read somewhere that if I load libraries into my main class (which I obviously do to open the window) then trying to run the program will fail on a 'main class not found' if I use them, same is also true for adding in any 'static final' members (which I use for the loggin of errors).

'Static Final' problem... I tried to adjust the classpath in ANT, and I get a load of other errors for the connection to Log4J and my 'wrapper' that I use (to do with it being a bad import), but the libraries exist where they should as set in the classpath).

I feel like I am almost there.... but not quite...

I'm doing this for the small 'library projects' that I am creating with the intention of using MAVAN for the main outer package that will connect them all together... for now however I just want to get this going so as it works.

I can supply the full source, or any other info as required.

Thanks in advance...

David

Answer

wallenborn picture wallenborn · Apr 2, 2012

It's simple when you know where to look. Say your META-INF/MANIFEST.MF contains the line:

Main-Class: mypackage.MyMainFile

then the structure of the jar needs to be

META-INF/MANIFEST.MF
mypackage/MyMainFile.class

where MyMainFile has to be in the proper package:

package mypackage;
public class MyMainFile {
  public static void main(String[] args) {
...

Your error message is caused by MyMainFile being in the wrong place.

Edit: it's been a while since the last time i did that with ant, but i think you need something like this: a source file structure that reflects the package struture, say

src/main/java/mypackage/MyMainFile.java

and a directory to put the compiled class file into, say

target

(I'm using maven conventions here, ant doesn't care and you can use the (rightclick)->properties->Java Build path->Sources tab in eclipse to set the source dir to src/main/java and the target to target/classes). Then in ant, have a compile target that compiles from source to target:

<target name="compile">
    <mkdir dir="target/classes"/>
    <javac srcdir="src/main/java" destdir="target/classes"/>
</target> 

so that after ant compile you should see the class file in the target

target/classes/mypackage/MyMainFile.class

Then have a ant jar task that packages this:

<target name="jar" depends="compile">
    <jar destfile="target/MyJarFile.jar" basedir="target/classes">
        <manifest>
            <attribute name="Main-Class" value="mypackage.MyMainFile"/>
        </manifest>
    </jar>
</target>

After saying ant compile jar you should have a file MyJarFile.jar inside target and

java -jar MyJarFile.jar

should run the main method.