Search code examples
javajarpackagingexecutable-jar

Executable Jar with depedencies


I'm trying to build an executable jar from the command line (I don't want to use ant or OneJar ) . Here is the content of my file.jar:

jar tvf file.jar
     0 Mon Sep 20 17:16:12 CEST 2010 lib/
 45396 Mon Sep 20 17:16:12 CEST 2010 lib/org.apache.commons.logging_1.0.4.v201005080501.jar
321330 Mon Sep 20 17:16:12 CEST 2010 lib/org.apache.commons.httpclient_3.1.0.v201005080502.jar
 55003 Mon Sep 20 17:16:12 CEST 2010 lib/org.apache.commons.codec_1.3.0.v20100518-1140.jar
     0 Mon Sep 20 19:15:00 CEST 2010 META-INF/
   265 Mon Sep 20 19:12:44 CEST 2010 META-INF/MANIFEST.MF
530609 Mon Sep 20 17:16:12 CEST 2010 ped.jar

the content of META-INF/MANIFEST.MF:

Manifest-Version: 1.0
Class-Path: ped.jar lib/org.apache.commons.codec_1.3.0.v20100518-1140.jar lib/org.apache.commons.httpclient_3.1.0.v201005080502.jar lib/org.apache.commons.logging_1.0.4.v201005080501.jar
Main-Class: fr.inserm.umr915.bomcat.ped.PedigreeDrawer

and the ped.jar do contains the Main-Class

jar tvf ped.jar | grep PedigreeDrawer.class
 39541 Mon Sep 20 17:16:10 CEST 2010 fr/inserm/umr915/bomcat/ped/PedigreeDrawer.class

but when I'm trying to execute the file.jar, I got an error:

java -jar file.jar
Exception in thread "main" java.lang.NoClassDefFoundError: fr/inserm/umr915/bomcat/ped/PedigreeDrawer
Caused by: java.lang.ClassNotFoundException: fr.inserm.umr915.bomcat.ped.PedigreeDrawer
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)

So, I'm missing something here. Can't I package an executable jar by just including the dependencies ?

Thanks for your help

Pierre


Solution

  • As already mentioned the jars in the Class-Path: attribute are relative to the loaction of the jar file and not inside the jar file. This is probably a remnant from the days Java was meant to run applets.

    You'll need to use a tool like maven uberjar to clobber all needed classes together in a single jar to create a standalone executable jar.

    You can of course also just unzip all jars in a single directory, add the META-INF and manifest and zip everything up again if you absolutely want to avoid using tools.