2012年10月25日 星期四

Executable Ant with other other jar in on jar with java.lang.NoClassDefFoundError.

1. Problem:
   How do you execute a jar file, and the other jar as the library in this executable jar? If you execute this executable jar with java -jar this.jar , it will appear a exception with java.lang.NoClassDefFoundError. But Why? it is also happened when you write down the Class-Path property with this reference library's jar in the manifest.mf file.

2. Why:
  Because JVM didn't execute a jar with loadding it's classpath in this jar,
it means even your jar has the lib folder with necessary jar, it didn't work.


3. Solution:
  (A) The simple way is to unzip the lib in jar, and then execute the jar. Because it extract the lib folder in the executable Jar, and use java to execute it, the classpath will reference lib/*, then all class will find in your executeaJar. The condition is you need wirte the reference lib in the manifest.mf.
        unzip canExecute.jar  lib/*;
        java -jar canExecute.jar;

  (B) Eclipse provide a exporting the executable jar function. Its method is include a classloader called JarRsrcLoader, and firstly jvm will load this loader which can load other jar. then load you class. But this method is depends on your IDE.

  (C) A package called One-Jar is support some solution. But it need modify your source call to load other jar file and your manifest.


Reference:
JVM with classloader
JVM with classloader written by caterpillar
JVM with claspath
One-JAR_intro
One_Jar_Office
ClasLoader with third-part
Manifest Intro 
Ant with manifest
Ant with manifest having lib
Ant with manifest_3_relavent the jar_other jar


沒有留言:

張貼留言