Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/lib/testlibrary/JavaToolUtils.java
38833 views
1
/*
2
* Copyright (c) 2014, 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. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
import java.io.BufferedReader;
27
import java.io.File;
28
import java.io.FileOutputStream;
29
import java.io.IOException;
30
import java.io.InputStreamReader;
31
import java.nio.file.Files;
32
import java.util.List;
33
import java.util.Objects;
34
import java.util.concurrent.TimeUnit;
35
import java.util.jar.Attributes;
36
import java.util.jar.JarEntry;
37
import java.util.jar.JarOutputStream;
38
import java.util.jar.Manifest;
39
import javax.tools.JavaCompiler;
40
import javax.tools.JavaFileObject;
41
import javax.tools.StandardJavaFileManager;
42
import javax.tools.ToolProvider;
43
44
/**
45
* Utils class for compiling , creating jar file and executing a java command
46
*
47
* @author Raghu Nair
48
*/
49
50
public class JavaToolUtils {
51
52
public static final long DEFAULT_WAIT_TIME = 10000;
53
54
private JavaToolUtils() {
55
}
56
57
/**
58
* Takes a list of files and compile these files into the working directory.
59
*
60
* @param files
61
* @throws IOException
62
*/
63
public static void compileFiles(List<File> files) throws IOException {
64
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
65
try (StandardJavaFileManager fileManager = compiler.
66
getStandardFileManager(null, null, null)) {
67
Iterable<? extends JavaFileObject> compilationUnit
68
= fileManager.getJavaFileObjectsFromFiles(files);
69
compiler.getTask(null, fileManager, null, null, null,
70
compilationUnit).call();
71
}
72
}
73
74
/**
75
* Create a jar file using the list of files provided.
76
*
77
* @param jar
78
* @param files
79
* @throws IOException
80
*/
81
public static void createJar(File jar, List<File> files)
82
throws IOException {
83
Manifest manifest = new Manifest();
84
manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION,
85
"1.0");
86
try (JarOutputStream target = new JarOutputStream(
87
new FileOutputStream(jar), manifest)) {
88
for (File file : files) {
89
add(file, target);
90
}
91
}
92
}
93
94
private static void add(File source, JarOutputStream target)
95
throws IOException {
96
Objects.requireNonNull(source, "source cannot be null");
97
Objects.requireNonNull(target, "target cannot be null");
98
// not tested against directories and from different path.
99
String name = source.getName();
100
if (source.isDirectory()) {
101
if (!name.isEmpty()) {
102
if (!name.endsWith("/")) {
103
name += "/";
104
}
105
JarEntry entry = new JarEntry(name);
106
entry.setTime(source.lastModified());
107
target.putNextEntry(entry);
108
target.closeEntry();
109
}
110
for (File nestedFile : source.listFiles()) {
111
add(nestedFile, target);
112
}
113
return;
114
}
115
System.out.println("Adding entry " + name);
116
JarEntry entry = new JarEntry(name);
117
entry.setTime(source.lastModified());
118
target.putNextEntry(entry);
119
Files.copy(source.toPath(), target);
120
target.closeEntry();
121
}
122
123
/**
124
* Runs java command with provided arguments. Caller should not pass java
125
* command in the argument list.
126
*
127
* @param commands
128
* @param waitTime time to wait for the command to exit in milli seconds
129
* @return
130
* @throws Exception
131
*/
132
public static int runJava(List<String> commands,long waitTime)
133
throws Exception {
134
String java = System.getProperty("java.home") + "/bin/java";
135
commands.add(0, java);
136
String command = commands.toString().replace(",", " ");
137
System.out.println("Executing the following command \n" + command);
138
ProcessBuilder processBuilder = new ProcessBuilder(commands);
139
final Process process = processBuilder.start();
140
BufferedReader errorStream = new BufferedReader(
141
new InputStreamReader(process.getErrorStream()));
142
BufferedReader outStream = new BufferedReader(
143
new InputStreamReader(process.getInputStream()));
144
String errorLine;
145
StringBuilder errors = new StringBuilder();
146
String outLines;
147
while ((errorLine = errorStream.readLine()) != null) {
148
errors.append(errorLine).append("\n");
149
}
150
while ((outLines = outStream.readLine()) != null) {
151
System.out.println(outLines);
152
}
153
errorLine = errors.toString();
154
System.err.println(errorLine);
155
process.waitFor(waitTime, TimeUnit.MILLISECONDS);
156
int exitStatus = process.exitValue();
157
if (exitStatus != 0 && errorLine != null && errorLine.isEmpty()) {
158
throw new RuntimeException(errorLine);
159
}
160
return exitStatus;
161
}
162
163
/**
164
* Runs java command with provided arguments. Caller should not pass java
165
* command in the argument list.
166
*
167
* @param commands
168
* @return
169
* @throws Exception
170
*/
171
public static int runJava(List<String> commands) throws Exception {
172
return runJava(commands, DEFAULT_WAIT_TIME);
173
}
174
175
/**
176
* Run any command
177
* @param commands
178
* @return
179
* @throws Exception
180
*/
181
public static int runCommand(List<String> commands) throws Exception {
182
String command = commands.toString().replace(",", " ");
183
System.out.println("Executing the following command \n" + command);
184
ProcessBuilder processBuilder = new ProcessBuilder(commands);
185
final Process process = processBuilder.start();
186
BufferedReader errorStream = new BufferedReader(
187
new InputStreamReader(process.getErrorStream()));
188
BufferedReader outStream = new BufferedReader(
189
new InputStreamReader(process.getInputStream()));
190
String errorLine;
191
StringBuilder errors = new StringBuilder();
192
String outLines;
193
while ((errorLine = errorStream.readLine()) != null) {
194
errors.append(errorLine).append("\n");
195
}
196
while ((outLines = outStream.readLine()) != null) {
197
System.out.println(outLines);
198
}
199
errorLine = errors.toString();
200
System.err.println(errorLine);
201
int exitStatus = process.exitValue();
202
if (exitStatus != 0 && errorLine != null && errorLine.isEmpty()) {
203
throw new RuntimeException(errorLine);
204
}
205
return exitStatus;
206
}
207
208
209
}
210
211