When starting a separate process which runs a program called Program.java
, I was wondering how I could add args
to this. For those of you who don't know, args
are the things you see at the start of lots of Java programs:
public static void main(String[] args)
I know when you run a .class
file from the terminal, you type java [program name] [args]
. So how do I add args
when starting a separate process? My code:
Class klass=Program.class;
String[] output=new String[2];
String javaHome = System.getProperty("java.home");
String javaBin = javaHome +
File.separator + "bin" +
File.separator + "java";
String classpath = System.getProperty("java.class.path");
String className = klass.getCanonicalName();
ProcessBuilder builder = new ProcessBuilder(
javaBin, "-cp", classpath, className);
builder.redirectErrorStream(true);
Process process = builder.start();
int in = -1;
InputStream is = process.getInputStream();
String[] outputs=new String[2];
try {
while ((in = is.read()) != -1) {
outputs[0]=outputs[0]+(char)in;
}
} catch (IOException ex) {
ex.printStackTrace();
}
builder.redirectErrorStream(true);
try {
while ((in = is.read()) != -1) {
outputs[1]=outputs[1]+(char)in;
}
} catch (IOException ex) {
ex.printStackTrace();
}
int exitCode = process.waitFor();
System.out.println("Exited with " + exitCode);
This differs from this question because my question uses ProcessBuilder
to create the process.
Thanks