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