Thursday, August 5, 2010

Creating an executable jar with dependent jars.

The following is skill you would require for packaging of a deliverable

  1. Set Java path like
          C:\setjdk16.bat
   2. Copy this java class into temporary dir
          cp Test.java C:\tmp\
   3. Switch to tmp directory
          cd C:\tmp
   4. Copy dependent jars dep1.jar dep2.jar C:\tmp
   5. Compile Java class
          javac -cp ./dep1.jar;./dep2.jar Test.java
   6. Create manifest.mft text file with contents like

Manifest-Version: 1.0
Main-Class: Test
Class-path: dep1.jar dep2.jar


   7. Archive contents
          jar cvfm myexe.jar manifest.mft *.class *.jar
   8. Run output jar like
          java -jar myexe.jar


To run with arguments
java -D -jar myexe.jar args[0] ...

Many a times even simple tend to forget this helpful concept

Note as on 17 OCT 2013 when using 1.7 JDK manifest.mft should look like else classloading of dependent jars fails  ( attribute Created-By mandatory )

Manifest-Version: 1.0
Main-Class: SSHCommandExecutorv2
Class-path: jsch-0.1.44.jar
Created-By: 1.7.0_06 (Oracle Corporation)

Followers