KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > jarlauncher > JarLauncher


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package org.lucane.jarlauncher;
20
21 import java.io.File JavaDoc;
22 import java.io.FilenameFilter JavaDoc;
23 import java.lang.reflect.InvocationTargetException JavaDoc;
24 import java.lang.reflect.Method JavaDoc;
25 import java.net.MalformedURLException JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.net.URLClassLoader JavaDoc;
28
29 public class JarLauncher
30 {
31   public static void main(String JavaDoc[] args)
32   throws Throwable JavaDoc
33   {
34     //check args
35
if(args.length < 2)
36     {
37         System.err.println("usage: JarLauncher <lib-path> <main-class> [params]");
38         System.exit(1);
39     }
40     
41     //read args
42
String JavaDoc path = args[0];
43     String JavaDoc mainClass = args[1];
44     String JavaDoc[] newArgs = new String JavaDoc[args.length-2];
45     for(int i=0;i<newArgs.length;i++)
46         newArgs[i] = args[i+2];
47         
48     //get jars names
49
File JavaDoc lib = new File JavaDoc(path);
50     File JavaDoc[] jars = lib.listFiles(new FilenameFilter JavaDoc() {
51         public boolean accept(File JavaDoc dir, String JavaDoc name) {
52             return name.toLowerCase().endsWith(".jar");
53         }
54     });
55     
56     //create url array
57
URL JavaDoc[] urls = new URL JavaDoc[jars.length];
58     for(int i=0;i<jars.length;i++)
59     {
60         try {
61             String JavaDoc fullPath = "jar:file:///" + jars[i].getAbsolutePath().replace('\\', '/') + "!/";
62             urls[i] = new URL JavaDoc(fullPath);
63         } catch (MalformedURLException JavaDoc e) {
64             System.err.println("Unable to load jar : " + jars[i].getName());
65         }
66     }
67     
68     //create classloader
69
ClassLoader JavaDoc loader = new URLClassLoader JavaDoc(urls);
70     
71     try {
72         //load main class
73
Class JavaDoc c = Class.forName(mainClass, true, loader);
74         Method JavaDoc m = c.getDeclaredMethod("main", new Class JavaDoc[]{String JavaDoc[].class});
75         m.invoke(null, new Object JavaDoc[]{newArgs});
76     } catch (ClassNotFoundException JavaDoc e) {
77         System.err.println("Unable to find class : " + mainClass);
78     } catch (SecurityException JavaDoc e) {
79         System.err.println("Unable to load main method in : " + mainClass);
80     } catch (NoSuchMethodException JavaDoc e) {
81         System.err.println("No main method in : " + mainClass);
82     } catch (IllegalArgumentException JavaDoc e) {
83         System.err.println("Wrong main method in : " + mainClass);
84     } catch (IllegalAccessException JavaDoc e) {
85         System.err.println("Wrong main method in : " + mainClass);
86     } catch (InvocationTargetException JavaDoc e) {
87         throw e.getCause();
88     }
89   }
90 }
91
92
Popular Tags