/* * Copyright 2005 NAKAMURA Minoru (nminoru@nminoru.jp) */ import java.io.File; import java.io.InputStream; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; import java.util.Vector; import java.lang.reflect.*; public class AppLoader extends java.net.URLClassLoader { private static Method loadClassMethod; static { // ClassLoader parent = ClassLoader.getSystemClassLoader().getParent(); try { loadClassMethod = ClassLoader.class.getDeclaredMethod("loadClass", new Class[] {String.class, Boolean.TYPE}); loadClassMethod.setAccessible(true); } catch(NoSuchMethodException e) { e.printStackTrace(); System.exit(1); } } /** * コンストラクタ */ protected AppLoader(URL[] urls) { super(urls); } /** * loadClass をオーバーライドして、システムクラスローダーを回避 */ protected final synchronized Class loadClass(String name, boolean resolve) throws ClassNotFoundException { Class c = findLoadedClass(name); if (c == null) { try { ClassLoader parent = getParent(); // assert (parent == ClassLoader.getSystemClassLoader()); parent = parent.getParent(); // assert (parent != null); c = (Class)loadClassMethod.invoke(parent, new Object[] {name, new Boolean(resolve)}); System.out.println(parent.getClass()); c = parent.loadClass(name); } catch (InvocationTargetException e) { if (e.getCause() instanceof ClassNotFoundException) { if (c == null) { c = findClass(name); } } else { e.printStackTrace(); System.exit(3); } } catch (IllegalAccessException e) { e.printStackTrace(); System.exit(3); } } return c; } /** * */ public static URL[] convertClasspathToURLs(String classpath) throws Exception { Vector tmpArray = new Vector(); URL[] urls = null; String[] parts = splitClasspath(classpath); for (int i=0 ; i 4) { final String postfix = fileName.substring(len - 4, len); if (postfix.equalsIgnoreCase(".jar") || postfix.equalsIgnoreCase(".zip")) { final String base = (new File(fileName).getCanonicalFile().toURL()).toString(); return new URL("jar:" + base + "!/"); } } } return null; } /** * */ protected void invokeMainMethod(String className, String[] args) throws Exception { // クラスの読み込み Class c = this.loadClass(className); Method method = getMainMethod(c); if (method != null) { method.invoke(null, new Object[] {args}); } } /** * */ protected Method getMainMethod(Class c) throws NoSuchMethodException { Method method = null; method = c.getMethod("main", new Class[] {String[].class}); return method; } // v1.3.1 でも動くように修正 protected static String[] splitClasspath(String classpath) { // try { // return classpath.split(File.pathSeparator); // } catch(PatternSyntaxException e) { // throw new IllegalArgumentException(); // } Vector temp = new Vector(); for(int i=0 ; i < classpath.length() ; ) { int ret = classpath.indexOf(File.pathSeparator, i); if (ret == -1) { temp.add(classpath.substring(i)); break; } else if (i == ret) { i += File.pathSeparator.length(); } else { String substring = classpath.substring(i, ret); temp.add(substring); i += substring.length() + File.pathSeparator.length(); } } // String[] arrays = new String[temp.size()]; for(int i=0 ; i < temp.size() ; i++) { arrays[i] = (String)temp.get(i); } return arrays; } /** * */ public static void main(String[] args) throws Exception { final int skip_args = 1; if (args.length >= skip_args) { final String javaClassPath = System.getProperty("java.class.path", ""); final URL[] urls = convertClasspathToURLs(javaClassPath); final int len = args.length - skip_args; String[] new_args = new String[len]; System.arraycopy(args, skip_args, new_args, 0, len); AppLoader app = new AppLoader(urls); app.invokeMainMethod(args[skip_args - 1], new_args); } else { System.out.println("Usage: ClassName [options ...]"); } } }