test/tools/javac/Paths/SameJVM.java

Wed, 06 Apr 2011 20:33:44 -0700

author
ohair
date
Wed, 06 Apr 2011 20:33:44 -0700
changeset 962
0ff2bbd38f10
parent 554
9d9f26857129
child 2525
2eb010b6cb22
permissions
-rw-r--r--

7033660: Update copyright year to 2011 on any files changed in 2011
Reviewed-by: dholmes

     1 /*
     2  * Copyright (c) 2004, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    22  */
    24 /*
    25  * Support routines to allow running `javac' or `jar' within the same JVM.
    26  */
    28 import java.io.*;
    29 import java.net.*;
    30 import java.lang.reflect.*;
    32 class SameJVM {
    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();
    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     }
    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 }

mercurial