Path: blob/master/test/jdk/sun/tools/jps/LingeredAppForJps.java
41149 views
/*1* Copyright (c) 2016, 2020, 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.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.BufferedWriter;24import java.io.File;25import java.io.FileWriter;26import java.io.IOException;27import java.util.ArrayList;28import java.util.List;29import jdk.test.lib.apps.LingeredApp;3031public class LingeredAppForJps extends LingeredApp {3233// if set, the app is run from jar file34private File jarFile;3536@Override37protected void runAddAppName(List<String> cmd) {38if (jarFile != null) {39cmd.add("-Xdiag");40cmd.add("-jar");41cmd.add(jarFile.getAbsolutePath());42} else {43super.runAddAppName(cmd);44}45}4647/**48* The jps output should contain processes' names49* (except when jps is started in quite mode).50* The expected name of the test process is prepared here.51*/52public String getProcessName() {53return jarFile == null54? getClass().getSimpleName()55: jarFile.getName();56}5758// full package name for the application's main class or the full path59// name to the application's JAR file:60public String getFullProcessName() {61return jarFile == null62? getClass().getCanonicalName()63: jarFile.getAbsolutePath();64}6566public void buildJar() throws IOException {67String className = LingeredAppForJps.class.getName();68File jar = new File(className + ".jar");69String testClassPath = System.getProperty("test.class.path", "?");7071// Classpath contains test class dir, libraries class dir(s), and72// may contains some additional dirs.73// We need to add to jar only classes from the test class directory.74// Main class (this class) should only be found in one directory75// from the classpath (test class dir), therefore only added once.76// Libraries class dir(s) and any additional classpath directories77// are written the jar manifest.7879File manifestFile = new File(className + ".mf");80String nl = System.getProperty("line.separator");81String manifestClasspath = "";8283List<String> jarArgs = new ArrayList<>();84jarArgs.add("-cfm");85jarArgs.add(jar.getAbsolutePath());86jarArgs.add(manifestFile.getAbsolutePath());87for (String path : testClassPath.split(File.pathSeparator)) {88String classFullName = path + File.separator + className + ".class";89File f = new File(classFullName);90if (f.exists()) {91jarArgs.add("-C");92jarArgs.add(path);93jarArgs.add(".");94System.out.println("INFO: scheduled to jar " + path);95} else {96manifestClasspath += " " + new File(path).toURI();97}98}99try (BufferedWriter output = new BufferedWriter(new FileWriter(manifestFile))) {100output.write("Main-Class: " + className + nl);101if (!manifestClasspath.isEmpty()) {102output.write("Class-Path: " + manifestClasspath + nl);103}104}105106System.out.println("Running jar " + jarArgs.toString());107sun.tools.jar.Main jarTool = new sun.tools.jar.Main(System.out, System.err, "jar");108if (!jarTool.run(jarArgs.toArray(new String[jarArgs.size()]))) {109throw new IOException("jar failed: args=" + jarArgs.toString());110}111112manifestFile.delete();113jar.deleteOnExit();114115// Print content of jar file116System.out.println("Content of jar file" + jar.getAbsolutePath());117118jarArgs = new ArrayList<>();119jarArgs.add("-tvf");120jarArgs.add(jar.getAbsolutePath());121122jarTool = new sun.tools.jar.Main(System.out, System.err, "jar");123if (!jarTool.run(jarArgs.toArray(new String[jarArgs.size()]))) {124throw new IOException("jar failed: args=" + jarArgs.toString());125}126127jarFile = jar;128}129130public static void main(String args[]) {131LingeredApp.main(args);132}133}134135136