Runtime.getRuntime().exec(java test)

Hi,

As part of a thesis for college, I am trying to execute a test java program. The program executes okay and I can read the results okay.

Sample code.

process = Runtime.getRuntime().exec("java test");

InputStream iOutStream = process.getInputStream();

InputStream ErrorStream = process.getErrorStream();

However, I need to be able to execute a progam that reads a parameter from the screen. I tried to pass a parameter file but it doesn't work.

process = Runtime.getRuntime().exec("java test <input.txt");

I also tried just passing the parameters as a string array but it doesn't work either.

process = Runtime.getRuntime().exec("java test 2, 2");

Is there any other way that I can get the process to accept parameters?

My deadline is looming so any help would be greatly appreciated.

Thanks

Eleanor

eleanmck@gofree.indigo.ie>

[962 byte] By [179] at [2007-9-26 1:29:58]
# 1
get the Outputstream of the process object and write to that.
esmo at 2007-6-29 1:27:23 > top of Java-index,Desktop,Runtime Environment...
# 2

Hi,

I got the output stream from the process and wrote to it but the program just hangs.

Sample code:

process = Runtime.getRuntime().exec("java test");

OutputStream iInStream = process.getOutputStream();

iInStream.write(5);

iInStream.flush();

Any suggestions?

Thanks

179 at 2007-6-29 1:27:23 > top of Java-index,Desktop,Runtime Environment...
# 3

I think you are waiting when reading the output before you write to the stream until it becomes NULL or -1. That will never finish if the program you execute is expecting input!

See a working example:

This class does nothing than print a line on the console, then waits for a line of input, then prints the result again on the console. This is the class you'd execute from some other java code:

import java.io.*;

public class Input {

public static void main(String args[]) {

try {

System.out.println("Before input");

BufferedReader r = new BufferedReader(new InputStreamReader(System.in));

String s=r.readLine();

System.out.println("Got line : " + s);

} catch(Exception e) {

System.out.println(e.getMessage());

}

}

}

The following 'controls' the input-test class above, so compile the classes and execute the following one:

import java.io.*;

import java.lang.*;

public class test {

public static void main(String args[]) {

try {

Process p = Runtime.getRuntime().exec("java Input");

BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));

BufferedWriter w = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));

String s=r.readLine(); // You cannot read until NULL because that will not happen before you have 'input' something

System.out.println("Got: " + s);

w.write("cvxcvxcvx\n\r");

w.flush();

System.out.println("waiting result:");

while( (s=r.readLine()) != null) { // read until finished.

System.out.println("after : " + s);

}

} catch(Exception e) {

System.out.println(e.getMessage());

}

}

}

esmo at 2007-6-29 1:27:23 > top of Java-index,Desktop,Runtime Environment...