blob: 664947b2577aa4e317c8dc727e0aaa0b405c324b [file] [log] [blame]
duke6e45e102007-12-01 00:00:00 +00001/*
lanaf3d11f62013-12-26 12:04:16 -08002 * Copyright (c) 2004, 2013, Oracle and/or its affiliates. All rights reserved.
duke6e45e102007-12-01 00:00:00 +00003 * 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 *
ohair2283b9d2010-05-25 15:58:33 -070019 * 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.
duke6e45e102007-12-01 00:00:00 +000022 */
23
24/**
25 *
26 */
27
28import java.io.*;
duke6e45e102007-12-01 00:00:00 +000029
30public class JavaProcess {
31
32 protected Process process = null;
33
34 private String classname;
35 private StringBuilder classArgs;
36 private StringBuilder javaOptions;
37
38 private static String java = System.getProperty("java.home")
39 + File.separator + "bin"
40 + File.separator + "java";
41
42 public JavaProcess(String classname) {
43 this(classname, "", "");
44 }
45
46 public JavaProcess(String classname, String classArgs) {
47 this(classname, "", classArgs);
48 }
49
50 public JavaProcess(String classname, String javaOptions, String classArgs) {
51 this.classname = classname;
52 this.javaOptions = new StringBuilder(javaOptions);
53 this.classArgs = new StringBuilder(classArgs);
54 }
55
56 /**
57 * add java options to the java command
58 */
59 public void addOptions(String[] opts) {
60 if (javaOptions != null && javaOptions.length() > 0) {
61 javaOptions.append(" ");
62 }
63
64 for (int i = 0; i < opts.length; i++) {
65 if (i != 0) {
66 javaOptions.append(" ");
67 }
68 javaOptions.append(opts[i]);
69 }
70 }
71
72 /**
73 * add arguments to the class arguments
74 */
75 public void addArguments(String[] args) {
76 if (classArgs != null && classArgs.length() > 0) {
77 classArgs.append(" ");
78 }
79
80 for (int i = 0; i < args.length; i++) {
81 if (i != 0) {
82 classArgs.append(" ");
83 }
84 classArgs.append(args[i]);
85 }
86 }
87
88 /**
89 * start the java process
90 */
91 public void start() throws IOException {
92 if (process != null) {
93 return;
94 }
95
96 String javaCommand = java + " " + javaOptions + " "
97 + classname + " " + classArgs;
98
99 System.out.println("exec'ing: " + javaCommand);
100
101 process = Runtime.getRuntime().exec(javaCommand);
102 }
103
104 /**
105 * destroy the java process
106 */
107 public void destroy() {
108 if (process != null) {
109 process.destroy();
110 }
111 process = null;
112 }
113
114 public int exitValue() {
115 if (process != null) {
116 return process.exitValue();
117 }
118 throw new RuntimeException("exitValue called with process == null");
119 }
120
121 public InputStream getErrorStream() {
122 if (process != null) {
123 return process.getErrorStream();
124 }
125 throw new RuntimeException(
126 "getErrorStream() called with process == null");
127 }
128
129 public InputStream getInputStream() {
130 if (process != null) {
131 return process.getInputStream();
132 }
133 throw new RuntimeException(
134 "getInputStream() called with process == null");
135 }
136
137 public OutputStream getOutputStream() {
138 if (process != null) {
139 return process.getOutputStream();
140 }
141 throw new RuntimeException(
142 "getOutputStream() called with process == null");
143 }
144
145 public int waitFor() throws InterruptedException {
146 if (process != null) {
147 return process.waitFor();
148 }
149 throw new RuntimeException("waitFor() called with process == null");
150 }
151}