URGENT: Process waiting for input

Hello people,

I have a serious problem with class Process.

how do I do to know if a process is waiting for input?

I create the process through the class ProcessBuilder:

String command ="java.exe <myClass>";

ProcessBuilder builder =new ProcessBuilder(command);

Process p = builder.start();

BufferedReader br =null;

try

{

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

}

catch(IOException ioe)

{

ioe.printStackTrace();

}

so, if myClass invoke the method readLine() of BufferedReader br, the process waiting for input.

how can I do therefore to intercept the state of my process for then to pass him input?

please, help me. I need...

thank you

gino.

if you have an example please send to 19gino86@fastwebnet.it or reply this topic.

[1216 byte] By [Gino86] at [2007-11-26 9:02:35]
# 1

Please refer to the techtip at:

http://java.sun.com/developer/JDCTechTips/2005/tt0727.html#2

Here's a simple use of ProcessBuilder that duplicates the functions of the DoRuntime example:

import java.io.*;

import java.util.*;

public class DoProcessBuilder {

public static void main(String args[]) throws IOException {

if (args.length <= 0) {

System.err.println("Need command to run");

System.exit(-1);

}

Process process = new ProcessBuilder(args).start();

InputStream is = process.getInputStream();

InputStreamReader isr = new InputStreamReader(is);

BufferedReader br = new BufferedReader(isr);

String line;

System.out.printf("Output of running %s is:",

Arrays.toString(args));

while ((line = br.readLine()) != null) {

System.out.println(line);

}

}

}

I think, in your code, instead of reading from System.in, process.getInputStream() should be read from...

KarthikR at 2007-7-6 23:10:25 > top of Java-index,Development Tools,Java Tools...
# 2
my problem isn't how to read the output of the process, but how to pass data to the process when it is in waiting state.I want to simulate a console
Gino86 at 2007-7-6 23:10:25 > top of Java-index,Development Tools,Java Tools...
# 3

I am sorry i misunderstood your original question. Are you trying to run a java class from within your app and pass input it?

According to:

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Process.html

...The created subprocess does not have its own terminal or console. All its standard io (i.e. stdin, stdout, stderr) operations will be redirected to the parent process through three streams (getOutputStream(), getInputStream(), getErrorStream()). The parent process uses these streams to feed input to and get output from the subprocess.

...

public abstract InputStream getInputStream()

...Gets the input stream of the subprocess. The stream obtains data piped from the standard output stream of the process represented by this Process object.

Instead of reading from System.in(), perhaps you can feed the input to the Process's outputstream...

Another wild idea: try 'System.setIn()' to redirect the standard input to your own input stream and feed the data to it. For instance, you can use a StringBufferInputStream as the redirected std input:

http://java.sun.com/j2se/1.5.0/docs/api/java/io/StringBufferInputStream.html

KarthikR at 2007-7-6 23:10:25 > top of Java-index,Development Tools,Java Tools...
# 4
can you post a simple example of this?in the API, StringBufferInputStream is Deprecated...thanks gino
Gino86 at 2007-7-6 23:10:25 > top of Java-index,Development Tools,Java Tools...
# 5
There are some sample codes in the following URLs: http://forum.java.sun.com/thread.jspa?forumID=32&threadID=691926 http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html http://forum.java.sun.com/thread.jspa?forumID=37&threadID=646096Do they
KarthikR at 2007-7-6 23:10:25 > top of Java-index,Development Tools,Java Tools...