what is correct internal structure of JAR file

亡梦爱人 提交于 2019-12-05 07:12:06
wallenborn

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!