隨筆-50  評論-55  文章-8  trackbacks-0
          前言:用myEclipse打包可執(zhí)行jar文件,特別是需要包含第三方資源包的時候,往往打包好之后jar文件找不到,即使 manifest.mf 文件設置了ClassPath= ?.jar 之類的,還會找不到第三方資源包的問題;一般情況下,第三方資源包都不應該和你的應用程序打成一個包,而是作為外部文件引入,這個時候需要考慮用classloader的方式打包。

          步驟一:
          ????? 自己的程序先打一個包my.jar(可以是不可執(zhí)行的),這個不包含第三方資源包(如log4j.jar);用myeclipse的export方式可以搞定;

          步驟二:
          ???? 寫一些加載第三方資源包的類,這里可以參考這個類(網上找的

          import java.io.*;??
          import java.net.*;??
          import java.lang.reflect.*;??
          ?
          public class BootLoader??
          {??
          ? public static void main(final String[] args) throws Exception??
          ? {??
          ??? // check that the lib folder exists??
          ??? File libRoot = new File(LIB_FOLDER);??
          ??? if(!libRoot.exists()) {??
          ????? throw new Exception("No 'lib' folder exists!");??
          ??? }??
          ??? // read all *.jar files in the lib folder to array??
          ??? File[] libs = libRoot.listFiles(new FileFilter()??
          ??? {??
          ????? public boolean accept(File dir)??
          ????? {??
          ??????? String name = dir.getName().toLowerCase();??
          ??????? return name.endsWith("jar") || name.endsWith("zip");??
          ????? }??
          ??? });??
          ?
          ??? URL[] urls = new URL[libs.length];??
          ??? // fill the urls array with URLs to library files found in libRoot??
          ??? for(int i = 0; i < libs.length; i++) {??
          ????? urls[i] = new URL("file",null,libs[i].getAbsolutePath());??
          ??? }??
          ??? // create a new classloader and use it to load our app.??
          ??? classLoader = new URLClassLoader(urls,??
          ???????????????????????????????????? Thread.currentThread().??
          ???????????????????????????????????? getContextClassLoader());??
          ??? // get the main method in our application to bring up the app.??
          ??? final Method mtd = classLoader.loadClass(APP_MAIN_CLASS).getMethod("main",??
          ??????? new Class[] {String[].class});??
          ??? // Using thread to launch the main 'loop' so that the current Main method??
          ??? // can return while the app is starting??
          ??? new Thread(new Runnable()??
          ??? {??
          ????? public void run()??
          ????? {??
          ??????? try {??
          ????????? mtd.invoke(null,new Object[] {args});??
          ??????? } // forward the args??
          ??????? catch(Exception e) {??
          ????????? throw new RuntimeException(e);??
          ??????? }??
          ????? }??
          ??? },"AppMain").start();??
          ??? // Give the app some time to start before returning from main.??
          ??? // This doesn't delay the starting in any way??
          ??? Thread.sleep(1000);??
          ? }??
          ?
          ? private static final String LIB_FOLDER = "lib";?? //所有第三方包放在這個文件夾下
          ? private static final String APP_MAIN_CLASS = "com.monitor.Main";?? //可執(zhí)行文件的入口程序,也是你自己代碼的一個包含main函數(shù)的類
          ? private static ClassLoader classLoader;??
          }?
          再將這個獨立的類打包成可執(zhí)行文件main.jar(不包含第三方資源包)

          步驟三:組織好各個文件
          比如在文件下F:/存在 文件
          main.jar
          lib(文件夾,lib文件夾下 放:my.jar ,log4j.jar,等等)
          config.properties(如果有配置文件)
          logo.jpg(如果有外部圖片)
          可以寫個批處理文件main.bat 內容: java? -Xms100m?? -Xmx800m? -jar main.jar
          雙擊main.bat 即可執(zhí)行
          posted on 2008-07-17 11:16 蔣家狂潮 閱讀(1862) 評論(1)  編輯  收藏 所屬分類: Basic

          評論:
          # re: 用classloader方式打包可執(zhí)行jar文件[未登錄] 2008-07-17 14:35 | paul
          你安裝fatjar,還用這么費勁啊  回復  更多評論
            
          主站蜘蛛池模板: 峨山| 合川市| 乐平市| 益阳市| 固安县| 呼和浩特市| 太仓市| 襄城县| 南充市| 吉木乃县| 壤塘县| 理塘县| 弥勒县| 加查县| 阆中市| 固始县| 许昌市| 甘南县| 洛浦县| 个旧市| 临高县| 宣城市| 昭苏县| 恩施市| 商都县| 越西县| 阳城县| 长武县| 河间市| 咸宁市| 治多县| 延吉市| 尖扎县| 瑞金市| 韶山市| 三原县| 岫岩| 行唐县| 阿拉善盟| 江孜县| 墨竹工卡县|