001: package com.reeltwo.jumble.util;
002:
003: import java.io.IOException;
004: import java.util.Properties;
005:
006: /**
007: * Class to run a java process with the same settings as this JRE.
008: *
009: * @author Tin Pavlinic
010: * @version $Revision: 516 $
011: */
012: public class JavaRunner {
013:
014: private final String mJvmBin;
015:
016: private String mClassName;
017:
018: private String[] mArgs;
019:
020: private String[] mJvmArgs;
021:
022: /**
023: * Constructor.
024: *
025: * @param className
026: * The name of the class to run
027: */
028: public JavaRunner(String className) {
029: this (className, new String[0]);
030: }
031:
032: /**
033: * Constructor
034: *
035: * @param className
036: * of the class to run
037: * @param arguments
038: * the arguments to pass to the main method.
039: */
040: public JavaRunner(String className, String[] arguments) {
041: mClassName = className;
042: mArgs = arguments;
043:
044: Properties props = System.getProperties();
045: String ls = props.getProperty("file.separator");
046: mJvmBin = props.getProperty("java.home") + ls + "bin" + ls
047: + "java";
048: final int initialLength = (JumbleUtils.isAssertionsEnabled() ? 3
049: : 2);
050:
051: mJvmArgs = new String[initialLength];
052: mJvmArgs[0] = "-cp";
053: mJvmArgs[1] = props.getProperty("java.class.path");
054: if (JumbleUtils.isAssertionsEnabled()) {
055: mJvmArgs[2] = "-ea";
056: }
057: }
058:
059: /**
060: * Gets the name of the class to run
061: *
062: * @return the class name
063: */
064: public String getClassName() {
065: return mClassName;
066: }
067:
068: /**
069: * Sets the class to run
070: *
071: * @param newName
072: * name of the new class to run. Must contain a main method.
073: */
074: public void setClassName(String newName) {
075: mClassName = newName;
076: }
077:
078: /**
079: * Gets the arguments to pass to the JVM.
080: *
081: * @return the arguments
082: */
083: public String[] getJvmArguments() {
084: return mJvmArgs;
085: }
086:
087: /**
088: * Sets the arguments to pass to the JVM. The default JVM arguments includes
089: * the classpath for the JVM to use, so if you supply new JVM arguments, you
090: * should probably include classpath settings.
091: *
092: * @param args
093: * the new arguments.
094: */
095: public void setJvmArguments(String[] args) {
096: mJvmArgs = args;
097: }
098:
099: /**
100: * Gets the arguments to pass to the main method
101: *
102: * @return the arguments
103: */
104: public String[] getArguments() {
105: return mArgs;
106: }
107:
108: /**
109: * Sets the arguments to pass to the main method
110: *
111: * @param args
112: * the new arguments.
113: */
114: public void setArguments(String[] args) {
115: mArgs = args;
116: }
117:
118: /**
119: * Starts the java process.
120: *
121: * @return the running java process
122: * @throws IOException
123: * if something goes wrong.
124: */
125: public Process start() throws IOException {
126: String[] command = getExecArgs();
127: //System.err.println(toString(command));
128: return Runtime.getRuntime().exec(command);
129: }
130:
131: /**
132: * Creates the actual arguments used to start the process. This incorporates
133: * the user-supplied arguments plus the explicit location of the JVM, and the
134: * classpath to supply.
135: *
136: * @return a <code>String[]</code> value
137: */
138: private String[] getExecArgs() {
139: final int baseArgs = 2 + (mJvmArgs == null ? 0
140: : mJvmArgs.length);
141:
142: // create the java command
143: String[] command = new String[baseArgs + getArguments().length];
144: int idx = 0;
145: command[idx++] = mJvmBin;
146: if (mJvmArgs != null) {
147: for (int i = 0; i < mJvmArgs.length; i++) {
148: command[idx++] = mJvmArgs[i];
149: }
150: }
151: command[idx++] = getClassName();
152: for (int i = 0; i < getArguments().length; i++) {
153: command[idx++] = getArguments()[i];
154: }
155: return command;
156: }
157:
158: private String toString(String[] command) {
159: StringBuffer sb = new StringBuffer();
160: for (int i = 0; i < command.length; i++) {
161: if (i != 0) {
162: sb.append(' ');
163: }
164: sb.append(command[i]);
165: }
166: return sb.toString();
167: }
168:
169: public String toString() {
170: return toString(getExecArgs());
171: }
172: }
|