what is correct internal structure of JAR file
Asked Answered
H

1

7

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

Harrell answered 2/4, 2012 at 10:38 Comment(1)
vikiiii I was going to put in a sly comment asking how many 'eyes' you have... but decided not to (notice 3 "." not one, not two). ;)Harrell
D
10

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.

Dross answered 2/4, 2012 at 10:52 Comment(4)
OK so how do I get ANT to give that type of structure in the JAR file? Or am I running ANt from the wrong location? (bear in mind that I am running the ANT task from within eclipse, not the CLI). And why is ANT creating the other type of directory structure (which is correct and how to specify one or the other, which I haven't found in the ANT docs... yet !Harrell
wallenborn currently in my "compile" I am pointing to a a destfile="${class}" and class = "build" , so is this where my problem is?? I'm going to change it and see what happens...Harrell
In the compile task, destfile is probably too specific anyway. You basically only want to tell the compile task: take everything in this src directory, recursively, compile it, and write the resulting classes into that dest directory, preserving subdirectories.Dross
OK... I think I'm getting somewhere. I created another 'hello world' project, and it compiles and runs fine. All the compile and build stuff JAR stuff is the same (with just the locations of the files being different due to a different parent directory. From what I can see the problem is that the /build/classes directory has no contents in my original project, but does in my 'hello World'... I just tried copying the files and descovered that I was 'cleaning' my files in 2 locations... so problem now solved. ThanksHarrell

© 2022 - 2024 — McMap. All rights reserved.