Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/lib/testlibrary/JavaToolUtils.java
38833 views
/*1* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425import java.io.BufferedReader;26import java.io.File;27import java.io.FileOutputStream;28import java.io.IOException;29import java.io.InputStreamReader;30import java.nio.file.Files;31import java.util.List;32import java.util.Objects;33import java.util.concurrent.TimeUnit;34import java.util.jar.Attributes;35import java.util.jar.JarEntry;36import java.util.jar.JarOutputStream;37import java.util.jar.Manifest;38import javax.tools.JavaCompiler;39import javax.tools.JavaFileObject;40import javax.tools.StandardJavaFileManager;41import javax.tools.ToolProvider;4243/**44* Utils class for compiling , creating jar file and executing a java command45*46* @author Raghu Nair47*/4849public class JavaToolUtils {5051public static final long DEFAULT_WAIT_TIME = 10000;5253private JavaToolUtils() {54}5556/**57* Takes a list of files and compile these files into the working directory.58*59* @param files60* @throws IOException61*/62public static void compileFiles(List<File> files) throws IOException {63JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();64try (StandardJavaFileManager fileManager = compiler.65getStandardFileManager(null, null, null)) {66Iterable<? extends JavaFileObject> compilationUnit67= fileManager.getJavaFileObjectsFromFiles(files);68compiler.getTask(null, fileManager, null, null, null,69compilationUnit).call();70}71}7273/**74* Create a jar file using the list of files provided.75*76* @param jar77* @param files78* @throws IOException79*/80public static void createJar(File jar, List<File> files)81throws IOException {82Manifest manifest = new Manifest();83manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION,84"1.0");85try (JarOutputStream target = new JarOutputStream(86new FileOutputStream(jar), manifest)) {87for (File file : files) {88add(file, target);89}90}91}9293private static void add(File source, JarOutputStream target)94throws IOException {95Objects.requireNonNull(source, "source cannot be null");96Objects.requireNonNull(target, "target cannot be null");97// not tested against directories and from different path.98String name = source.getName();99if (source.isDirectory()) {100if (!name.isEmpty()) {101if (!name.endsWith("/")) {102name += "/";103}104JarEntry entry = new JarEntry(name);105entry.setTime(source.lastModified());106target.putNextEntry(entry);107target.closeEntry();108}109for (File nestedFile : source.listFiles()) {110add(nestedFile, target);111}112return;113}114System.out.println("Adding entry " + name);115JarEntry entry = new JarEntry(name);116entry.setTime(source.lastModified());117target.putNextEntry(entry);118Files.copy(source.toPath(), target);119target.closeEntry();120}121122/**123* Runs java command with provided arguments. Caller should not pass java124* command in the argument list.125*126* @param commands127* @param waitTime time to wait for the command to exit in milli seconds128* @return129* @throws Exception130*/131public static int runJava(List<String> commands,long waitTime)132throws Exception {133String java = System.getProperty("java.home") + "/bin/java";134commands.add(0, java);135String command = commands.toString().replace(",", " ");136System.out.println("Executing the following command \n" + command);137ProcessBuilder processBuilder = new ProcessBuilder(commands);138final Process process = processBuilder.start();139BufferedReader errorStream = new BufferedReader(140new InputStreamReader(process.getErrorStream()));141BufferedReader outStream = new BufferedReader(142new InputStreamReader(process.getInputStream()));143String errorLine;144StringBuilder errors = new StringBuilder();145String outLines;146while ((errorLine = errorStream.readLine()) != null) {147errors.append(errorLine).append("\n");148}149while ((outLines = outStream.readLine()) != null) {150System.out.println(outLines);151}152errorLine = errors.toString();153System.err.println(errorLine);154process.waitFor(waitTime, TimeUnit.MILLISECONDS);155int exitStatus = process.exitValue();156if (exitStatus != 0 && errorLine != null && errorLine.isEmpty()) {157throw new RuntimeException(errorLine);158}159return exitStatus;160}161162/**163* Runs java command with provided arguments. Caller should not pass java164* command in the argument list.165*166* @param commands167* @return168* @throws Exception169*/170public static int runJava(List<String> commands) throws Exception {171return runJava(commands, DEFAULT_WAIT_TIME);172}173174/**175* Run any command176* @param commands177* @return178* @throws Exception179*/180public static int runCommand(List<String> commands) throws Exception {181String command = commands.toString().replace(",", " ");182System.out.println("Executing the following command \n" + command);183ProcessBuilder processBuilder = new ProcessBuilder(commands);184final Process process = processBuilder.start();185BufferedReader errorStream = new BufferedReader(186new InputStreamReader(process.getErrorStream()));187BufferedReader outStream = new BufferedReader(188new InputStreamReader(process.getInputStream()));189String errorLine;190StringBuilder errors = new StringBuilder();191String outLines;192while ((errorLine = errorStream.readLine()) != null) {193errors.append(errorLine).append("\n");194}195while ((outLines = outStream.readLine()) != null) {196System.out.println(outLines);197}198errorLine = errors.toString();199System.err.println(errorLine);200int exitStatus = process.exitValue();201if (exitStatus != 0 && errorLine != null && errorLine.isEmpty()) {202throw new RuntimeException(errorLine);203}204return exitStatus;205}206207208}209210211