hotspot/test/runtime/SharedArchiveFile/BasicJarBuilder.java
changeset 32179 246b2ef581b2
child 32615 b6c0aafd6945
equal deleted inserted replaced
32078:8757adb98184 32179:246b2ef581b2
       
     1 /*
       
     2  * Copyright (c) 2015, 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  */
       
    23 
       
    24 /*
       
    25  * @summary Simple jar builder
       
    26  *   Input: jarName className1 className2 ...
       
    27  *     do not specify extensions, just the names
       
    28  *     E.g. prot_domain ProtDomainA ProtDomainB
       
    29  *   Output: A jar containing compiled classes, placed in a test classes folder
       
    30  */
       
    31 
       
    32 import jdk.test.lib.*;
       
    33 
       
    34 import java.io.File;
       
    35 import java.util.ArrayList;
       
    36 import sun.tools.jar.Main;
       
    37 
       
    38 public class BasicJarBuilder {
       
    39     private static final String classDir = System.getProperty("test.classes");
       
    40 
       
    41     public static void build(String jarName, String ...classNames)
       
    42         throws Exception {
       
    43 
       
    44         createSimpleJar(classDir, classDir + File.separator + jarName +
       
    45             ".jar", classNames);
       
    46     }
       
    47 
       
    48     private static void createSimpleJar(String jarclassDir, String jarName,
       
    49         String[] classNames) throws Exception {
       
    50         ArrayList<String> args = new ArrayList<String>();
       
    51         args.add("cf");
       
    52         args.add(jarName);
       
    53         addClassArgs(args, jarclassDir, classNames);
       
    54         createJar(args);
       
    55     }
       
    56 
       
    57     private static void addClassArgs(ArrayList<String> args, String jarclassDir,
       
    58         String[] classNames) {
       
    59 
       
    60         for (String name : classNames) {
       
    61             args.add("-C");
       
    62             args.add(jarclassDir);
       
    63             args.add(name + ".class");
       
    64         }
       
    65     }
       
    66 
       
    67     private static void createJar(ArrayList<String> args) {
       
    68         Main jarTool = new Main(System.out, System.err, "jar");
       
    69         if (!jarTool.run(args.toArray(new String[1]))) {
       
    70             throw new RuntimeException("jar operation failed");
       
    71         }
       
    72     }
       
    73 
       
    74     // helpers
       
    75     public static String getTestJar(String jar) {
       
    76         File dir = new File(System.getProperty("test.classes", "."));
       
    77         File jarFile = new File(dir, jar);
       
    78         if (!jarFile.exists()) {
       
    79             throw new RuntimeException("Cannot find " + jarFile.getPath());
       
    80         }
       
    81         if (!jarFile.isFile()) {
       
    82             throw new RuntimeException("Not a regular file: " + jarFile.getPath());
       
    83         }
       
    84         return jarFile.getPath();
       
    85     }
       
    86 }