146 }
147
148 public static ProcessBuilder createJavaProcessBuilder(boolean addTestVmOptions, String... command) throws Exception {
149 String javapath = JDKToolFinder.getJDKTool("java");
150
151 ArrayList<String> args = new ArrayList<>();
152 args.add(javapath);
153 Collections.addAll(args, getPlatformSpecificVMArgs());
154
155 if (addTestVmOptions) {
156 String vmopts = System.getProperty("test.vm.opts");
157 if (vmopts != null && vmopts.length() > 0) {
158 Collections.addAll(args, vmopts.split("\\s"));
159 }
160 }
161
162 Collections.addAll(args, command);
163
164 // Reporting
165 StringBuilder cmdLine = new StringBuilder();
166 for (String cmd : args)
167 cmdLine.append(cmd).append(' ');
168 System.out.println("Command line: [" + cmdLine.toString() + "]");
169
170 return new ProcessBuilder(args.toArray(new String[args.size()]));
171 }
172 }
|
146 }
147
148 public static ProcessBuilder createJavaProcessBuilder(boolean addTestVmOptions, String... command) throws Exception {
149 String javapath = JDKToolFinder.getJDKTool("java");
150
151 ArrayList<String> args = new ArrayList<>();
152 args.add(javapath);
153 Collections.addAll(args, getPlatformSpecificVMArgs());
154
155 if (addTestVmOptions) {
156 String vmopts = System.getProperty("test.vm.opts");
157 if (vmopts != null && vmopts.length() > 0) {
158 Collections.addAll(args, vmopts.split("\\s"));
159 }
160 }
161
162 Collections.addAll(args, command);
163
164 // Reporting
165 StringBuilder cmdLine = new StringBuilder();
166 for (String cmd : args) {
167 cmdLine.append(cmd).append(' ');
168 }
169 System.out.println("Command line: [" + cmdLine.toString() + "]");
170
171 return new ProcessBuilder(args.toArray(new String[args.size()]));
172 }
173
174 /**
175 * Executes a test jvm process, waits for it to finish and returns the process output.
176 * The default jvm options from jtreg, test.vm.opts and test.java.opts, are added.
177 * The java from the test.jdk is used to execute the command.
178 *
179 * The command line will be like:
180 * {test.jdk}/bin/java {test.vm.opts} {test.java.opts} cmds
181 *
182 * @param cmds User specifed arguments.
183 * @return The output from the process.
184 */
185 public static OutputAnalyzer executeTestJvm(String... cmds) throws Throwable {
186 ProcessBuilder pb = createJavaProcessBuilder(Utils.addTestJavaOpts(cmds));
187 return executeProcess(pb);
188 }
189
190 /**
191 * Executes a process, waits for it to finish and returns the process output.
192 * @param pb The ProcessBuilder to execute.
193 * @return The output from the process.
194 */
195 public static OutputAnalyzer executeProcess(ProcessBuilder pb) throws Throwable {
196 OutputAnalyzer output = null;
197 try {
198 output = new OutputAnalyzer(pb.start());
199 return output;
200 } catch (Throwable t) {
201 System.out.println("executeProcess() failed: " + t);
202 throw t;
203 } finally {
204 System.out.println(getProcessLog(pb, output));
205 }
206 }
207
208 /**
209 * Executes a process, waits for it to finish and returns the process output.
210 * @param cmds The command line to execute.
211 * @return The output from the process.
212 */
213 public static OutputAnalyzer executeProcess(String... cmds) throws Throwable {
214 return executeProcess(new ProcessBuilder(cmds));
215 }
216
217 /**
218 * Used to log command line, stdout, stderr and exit code from an executed process.
219 * @param pb The executed process.
220 * @param output The output from the process.
221 */
222 public static String getProcessLog(ProcessBuilder pb, OutputAnalyzer output) {
223 String stderr = output == null ? "null" : output.getStderr();
224 String stdout = output == null ? "null" : output.getStdout();
225 String exitValue = output == null ? "null": Integer.toString(output.getExitValue());
226 StringBuilder logMsg = new StringBuilder();
227 final String nl = System.getProperty("line.separator");
228 logMsg.append("--- ProcessLog ---" + nl);
229 logMsg.append("cmd: " + getCommandLine(pb) + nl);
230 logMsg.append("exitvalue: " + exitValue + nl);
231 logMsg.append("stderr: " + stderr + nl);
232 logMsg.append("stdout: " + stdout + nl);
233 return logMsg.toString();
234 }
235
236 /**
237 * @return The full command line for the ProcessBuilder.
238 */
239 public static String getCommandLine(ProcessBuilder pb) {
240 if (pb == null) {
241 return "null";
242 }
243 StringBuilder cmd = new StringBuilder();
244 for (String s : pb.command()) {
245 cmd.append(s).append(" ");
246 }
247 return cmd.toString().trim();
248 }
249 }
|