1 /*
   2  * Copyright (c) 2015, 2016, 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 java.io.File;
  33 import java.util.ArrayList;
  34 import sun.tools.jar.Main;
  35 
  36 // Using JarBuilder requires that all to-be-jarred classes should be placed
  37 // in the current working directory, aka "."
  38 public class BasicJarBuilder {
  39     private static final String classDir = System.getProperty("test.classes");
  40 
  41     public static void build(boolean classesInWorkDir, String jarName,
  42         String ...classNames) throws Exception {
  43 
  44         if (classesInWorkDir) {
  45             createSimpleJar(".", classDir + File.separator + jarName + ".jar", classNames);
  46         } else {
  47             build(jarName, classNames);
  48         }
  49     }
  50 
  51     public static void build(String jarName, String ...classNames) throws Exception {
  52         createSimpleJar(classDir, classDir + File.separator + jarName + ".jar",
  53             classNames);
  54     }
  55 
  56     private static void createSimpleJar(String jarclassDir, String jarName,
  57         String[] classNames) throws Exception {
  58         ArrayList<String> args = new ArrayList<String>();
  59         args.add("cf");
  60         args.add(jarName);
  61         addClassArgs(args, jarclassDir, classNames);
  62         createJar(args);
  63     }
  64 
  65     private static void addClassArgs(ArrayList<String> args, String jarclassDir,
  66         String[] classNames) {
  67 
  68         for (String name : classNames) {
  69             args.add("-C");
  70             args.add(jarclassDir);
  71             args.add(name + ".class");
  72         }
  73     }
  74 
  75     private static void createJar(ArrayList<String> args) {
  76         Main jarTool = new Main(System.out, System.err, "jar");
  77         if (!jarTool.run(args.toArray(new String[1]))) {
  78             throw new RuntimeException("jar operation failed");
  79         }
  80     }
  81 
  82     // Get full path to the test jar
  83     public static String getTestJar(String jar) {
  84         File dir = new File(System.getProperty("test.classes", "."));
  85         File jarFile = new File(dir, jar);
  86         if (!jarFile.exists()) {
  87             throw new RuntimeException("Cannot find " + jarFile.getPath());
  88         }
  89         if (!jarFile.isFile()) {
  90             throw new RuntimeException("Not a regular file: " + jarFile.getPath());
  91         }
  92         return jarFile.getPath();
  93     }
  94 }