01: /*
02: * @(#)MyClassPath.java 1.2 06/10/11
03: *
04: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
05: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
06: *
07: * This program is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU General Public License version
09: * 2 only, as published by the Free Software Foundation.
10: *
11: * This program is distributed in the hope that it will be useful, but
12: * WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * General Public License version 2 for more details (a copy is
15: * included at /legal/license.txt).
16: *
17: * You should have received a copy of the GNU General Public License
18: * version 2 along with this work; if not, write to the Free Software
19: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20: * 02110-1301 USA
21: *
22: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
23: * Clara, CA 95054 or visit www.sun.com if you need additional
24: * information or have any questions.
25: *
26: */
27:
28: package sun.tools;
29:
30: import java.util.ArrayList;
31: import java.util.HashSet;
32: import java.util.Iterator;
33: import java.security.ProtectionDomain;
34: import java.security.CodeSource;
35: import java.net.URL;
36: import java.net.URLClassLoader;
37: import java.lang.reflect.Method;
38: import java.lang.reflect.InvocationTargetException;
39:
40: public class MyClassPath {
41: public static void main(String[] argv) throws Throwable {
42:
43: if (argv.length == 0) {
44: System.err.println("usage: classname [args...]");
45: return;
46: }
47:
48: Class me = sun.tools.MyClassPath.class;
49: URL url = me.getProtectionDomain().getCodeSource()
50: .getLocation();
51: URL[] urls = new URL[] { url };
52:
53: ClassLoader filter = new ClassLoader() {
54: protected synchronized Class loadClass(String name,
55: boolean resolve) throws ClassNotFoundException {
56: if (name.startsWith("java.")) {
57: return super .loadClass(name, resolve);
58: } else {
59: throw new ClassNotFoundException(name);
60: }
61: }
62: };
63:
64: ClassLoader l = new URLClassLoader(urls, filter);
65:
66: {
67: Class mainClass = l.loadClass(argv[0]);
68: String[] args = new String[argv.length - 1];
69: System.arraycopy(argv, 1, args, 0, argv.length - 1);
70: Class[] argsType = { args.getClass() };
71: Method mainMethod = mainClass.getMethod("main", argsType);
72: Object[] args2 = { args };
73: try {
74: mainMethod.invoke(null, args2);
75: } catch (InvocationTargetException i) {
76: throw i.getTargetException();
77: }
78: }
79: }
80: }
|