开发者

Nested jar files

开发者 https://www.devze.com 2023-02-06 21:31 出处:网络
a question on building executable jar files. Suppose we have a jar file a.jar that contains UI.class. I have used a metafile in archiving a.jar that says

a question on building executable jar files. Suppose we have a jar file a.jar that contains UI.class. I have used a metafile in archiving a.jar that says

Main-Class: UI
Class-Path: . b.jar c.jar

Now, if I want to ship this executable a.jar to somebody, I have to ship 3 files - a.jar, b.jar and c.jar. The user has to put all 3 in the same folder, and then double clicking on a.jar would work. It would be simpler if I could ship a single file (a.jar), and the JVM could figure out to extract b.jar and c.jar from in there to use the classes. These 2 are external libraries and I dont want to expand them and re-jar the class files in a.jar.

Can this be done? If so, what is the jar command and wh开发者_JS百科at should go in the metafile? Currently the command is

jar cmf metafile a.jar UI.class

Thank you.


Have you looked at this tool OneJar? Also this is a good article to read on OneJar.

If you decide to not use an external tool then your alternative is to put the library/helper jars in the classpath in the manifest, and then copy the jars themselves in a directory path relative to your main jar.

EDIT: OP asked for an example MANIFEST.MF. I coped this from the example One-Jar example jar file.

Manifest-Version: 1.0
Main-Class: com.simontuffs.onejar.Boot
One-Jar-Expand: expand,doc


There is OneJar as people have mentioned but also (especially if you are using eclipse) you can consider fatjar.

ALternatively, you can also achieve this by using an ANT Task here's an example:

<target name="dist" depends="compile,static" description="Compiles and builds jar files">

    <mkdir dir="${dist}"/>

    <jar destfile="${dist}/MYAPP.jar">
        <zipfileset src="${dist}/MY_OTHER_APP.jar"/>    
        <zipfileset src="${lib}/commons-io-1.4/commons-io-1.4.jar"/>
        <zipfileset src="${lib}/commons-math-2.1/commons-math-2.1.jar"/>

        <fileset dir="${res}" includes="*"/>

        <manifest>
            <attribute name="Main-Class" value=<where your main class is>/>
        </manifest>
    </jar>
</target>


OneJar is good for just bundling a few things together, but it's generally quite slow and inflexible. This isn't a dig at OneJar, it's great for what it does but unless I wanted a quick and easy distributable for a tiny program I wouldn't use it.

I did use it for a while but then switched to izpack. It provides an installer as a single jar which I've found to generally work very well, and supports things such as windows specific shortcuts as well. It's also really easy to integrate it with ant.


Another way can be use one of several available tools to create a setup deliverable and your setup pack will unpack your application when installed.


Maven users can also use the assembly plugin for this: http://maven.apache.org/plugins/maven-assembly-plugin/usage.html

0

精彩评论

暂无评论...
验证码 换一张
取 消