1 /*
2  * Copyright (c) 2019, 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 import jdk.test.lib.util.JarUtils;
25 
26 import java.io.File;
27 import java.io.IOException;
28 import java.nio.file.Path;
29 import java.nio.file.Paths;
30 import java.util.jar.Attributes;
31 import java.util.jar.Manifest;
32 
33 /**
34  * Launches a new Java process using -jar Java option.
35  */
36 
37 public class TestProcessJarLauncher extends TestProcessLauncher {
38 
39     private static final String JAR_FILE = "testprocess.jar";
40 
41 
TestProcessJarLauncher(String className)42     public TestProcessJarLauncher(String className) {
43         super(className);
44     }
45 
prepareLaunch(String javaExec, String pipePort)46     protected String prepareLaunch(String javaExec, String pipePort) {
47         try {
48             File jarFile = prepareJar();
49             return javaExec + " -jar " + jarFile.getAbsolutePath() + " -pipe.port=" + pipePort;
50         } catch (IOException e) {
51             throw new RuntimeException("Failed to prepare a jar file", e);
52         }
53     }
54 
prepareJar()55     private File prepareJar() throws IOException {
56         Path jarFile = USER_DIR.resolve(JAR_FILE);
57         Manifest manifest = createManifest();
58         Path testClass = TEST_CLASSES_DIR.resolve(className + ".class");
59         JarUtils.createJarFile(jarFile, manifest, TEST_CLASSES_DIR, Paths.get("."));
60         return jarFile.toFile();
61     }
62 
createManifest()63     private Manifest createManifest() {
64         Manifest manifest = new Manifest();
65         manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
66         manifest.getMainAttributes().put(Attributes.Name.MAIN_CLASS, className);
67         return manifest;
68     }
69 
getJarFile()70     public String getJarFile() {
71         return JAR_FILE;
72     }
73 }
74