|
1 /* |
|
2 * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
20 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
21 * have any questions. |
|
22 */ |
|
23 |
|
24 /* |
|
25 * Support routines to allow running `javac' or `jar' within the same JVM. |
|
26 */ |
|
27 |
|
28 import java.io.*; |
|
29 import java.net.*; |
|
30 import java.lang.reflect.*; |
|
31 |
|
32 class SameJVM { |
|
33 |
|
34 private static ClassLoader toolsClassLoader() { |
|
35 File javaHome = new File(System.getProperty("java.home")); |
|
36 File classesDir = new File(javaHome, "classes"); |
|
37 File libDir = new File(javaHome.getParentFile(), "lib"); |
|
38 File toolsJar = new File(libDir, "tools.jar"); |
|
39 try { |
|
40 return new URLClassLoader( |
|
41 new URL[] {classesDir.toURL(), toolsJar.toURL()}); |
|
42 } catch (MalformedURLException e) { throw new AssertionError(e); } |
|
43 } |
|
44 private static final ClassLoader cl = toolsClassLoader(); |
|
45 |
|
46 static void javac(String... args) throws Exception { |
|
47 Class c = Class.forName("com.sun.tools.javac.Main", true, cl); |
|
48 int status = (Integer) |
|
49 c.getMethod("compile", new Class[] {String[].class}) |
|
50 .invoke(c.newInstance(), new Object[] {args}); |
|
51 if (status != 0) |
|
52 throw new Exception("javac failed: status=" + status); |
|
53 } |
|
54 |
|
55 static void jar(String... args) throws Exception { |
|
56 Class c = Class.forName("sun.tools.jar.Main", true, cl); |
|
57 Object instance = c.getConstructor( |
|
58 new Class[] {PrintStream.class, PrintStream.class, String.class}) |
|
59 .newInstance(System.out, System.err, "jar"); |
|
60 boolean result = (Boolean) |
|
61 c.getMethod("run", new Class[] {String[].class}) |
|
62 .invoke(instance, new Object[] {args}); |
|
63 if (! result) |
|
64 throw new Exception("jar failed"); |
|
65 } |
|
66 } |