Monday, October 02, 2006

BLOG: Deliver Your Java Application in One-JAR!

Deliver Your Java Application in One-JAR!

      $ jar -tf myapp.jar

      META-INF/MANIFEST.MF

      lib/a.jar

      lib/b.jar

main/main.jar

The manifest contains a Main-Class setting to launch our application, and entries for the supporting JAR files a.jar and b.jar.

      Manifest-Version: 1.0

      Class-Path: main/main.jar lib/a.jar lib/b.jar

      Main-Class: com.main.Main


However, if file system is not defined, you will get errors. What about using the 'jar:' protocol in the Class-Path entry? This doesn't work either. It appears that the JAR loader only supports file-based URLS. Which is where One-JAR comes in.

      $ jar -tf one-jar-boot.jar

      META-INF/MANIFEST.MF

      com/simontuffs/onejar/Boot.class

      com/simontuffs/onejar/Handler$1.class

      com/simontuffs/onejar/Handler.class

      com/simontuffs/onejar/JarClassLoader$ByteCode.class

      com/simontuffs/onejar/JarClassLoader.class

      boot-manifest.mf

There is also a pre-built manifest file called boot-manifest.mf. To complete the One-JAR deployment process, create a suitable directory and expand this file into it, then update the myapp.jar file as follows:

      $ mkdir boot

      $ cd boot

      $ jar -xvf ../one-jar-boot.jar

      $ jar -uvfm ../myapp.jar boot-manifest.mf .

-Done-jar.info: info

-Done-jar.verbose: VERBOSE

Notice how the JarClassLoader keeps track of where classes came from: for example the com.a.A class is contained inside the lib/a.jar file inside myapp.jar.

At this stage you should be asking "How does One-JAR decide which is the main class?". The conventional way would be to have a manifest attribute in the top-level JAR file myapp.jar and require this to be edited before the final jar was assembled. But this is all unnecessary: One-JAR simply looks for a jar file inside the main sub-directory of the composite JAR file myapp.jar, and provided that that JAR file has a Main-Class manifest attribute, it will be used as the main entry point. This allows an existing main-class JAR file to be bundled inside a One-JAR archive without further modification!

http://one-jar.sourceforge.net/

No comments:

Thumbs Up to GitHub Copilot and JetBrains Resharper

Having used AI tool GitHub Copilot since 08/16/2023, I’ve realized that learning GitHub Copilot is like learning a new framework or library ...