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

          步驟一:
          ????? 自己的程序先打一個包my.jar(可以是不可執行的),這個不包含第三方資源包(如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";?? //可執行文件的入口程序,也是你自己代碼的一個包含main函數的類
          ? private static ClassLoader classLoader;??
          }?
          再將這個獨立的類打包成可執行文件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 即可執行
          posted on 2008-07-17 11:16 蔣家狂潮 閱讀(1856) 評論(1)  編輯  收藏 所屬分類: Basic

          評論:
          # re: 用classloader方式打包可執行jar文件[未登錄] 2008-07-17 14:35 | paul
          你安裝fatjar,還用這么費勁啊  回復  更多評論
            
          主站蜘蛛池模板: 天柱县| 独山县| 抚宁县| 旺苍县| 怀安县| 奉化市| 绥宁县| 长岛县| 济源市| 桐乡市| 临猗县| 金堂县| 突泉县| 普格县| 石台县| 松原市| 娱乐| 平原县| 延安市| 克东县| 林芝县| 安新县| 都昌县| 达州市| 康保县| 内丘县| 瓦房店市| 永丰县| 宜城市| 威海市| 勐海县| 名山县| 慈利县| 祁东县| 安丘市| 兴和县| 库车县| 南丹县| 昔阳县| 永寿县| 泰州市|