Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/serviceability/tmtools/share/common/ToolRunner.java
41159 views
1
/*
2
* Copyright (c) 2015, 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
package common;
24
25
import java.nio.file.Files;
26
import java.nio.file.Path;
27
import java.nio.file.Paths;
28
import java.util.Arrays;
29
import java.time.Instant;
30
31
/**
32
* This class starts a process specified by the passed command line waits till
33
* the process completes and returns the process exit code and stdout and stderr
34
* output as ToolResults
35
*/
36
class ToolRunner {
37
private final String[] cmdArgs;
38
39
ToolRunner(String cmdLine) {
40
cmdArgs = cmdLine.split(" +");
41
}
42
43
/**
44
* Starts the process, waits for the process completion and returns the
45
* results
46
*
47
* @return process results
48
* @throws Exception if anything goes wrong
49
*/
50
ToolResults runToCompletion() throws Exception {
51
ProcessBuilder pb = new ProcessBuilder(cmdArgs);
52
Path out = Files.createTempFile(Paths.get("."), "out.", ".txt");
53
Path err = out.resolveSibling(out.getFileName().toString().replaceFirst("out", "err"));
54
55
Process p = pb.redirectOutput(ProcessBuilder.Redirect.to(out.toFile()))
56
.redirectError(ProcessBuilder.Redirect.to(err.toFile()))
57
.start();
58
System.out.printf("[%s] started process %d %s with out/err redirected to '%s' and '%s'%n",
59
Instant.now().toString(), p.pid(), pb.command(), out.toString(), err.toString());
60
int exitCode = p.waitFor();
61
System.out.printf("[%s] process %d finished with exit code = %d%n",
62
Instant.now().toString(), p.pid(), exitCode);
63
return new ToolResults(exitCode, Files.readAllLines(out), Files.readAllLines(err));
64
}
65
}
66
67