I'v been perusing the Java developers forums trying to figure out the answer to my question. I have gotten pretty far, but need a bit more.
am trying to design a program that will control another program(java). It will do this by reading the output of the program and then sending it information as if a user had typed it in.
I can create the input and output streams just fine, but because of the way processes work in Java, I need to control the input and output streams themselvses.
Here is an example of the small controller I am using in place of my big one for now:
import java.util.*;
import java.io.*;
public class MediocreExecJavac
{
public static void main(String args[])
{
try
{
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("java sampleTool");
BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line = null;
System.out.println("<ERROR>");
while ( (line = br.readLine()) != null)
System.out.println(line);
System.out.println("</ERROR>");
int exitVal = proc.waitFor();
System.out.println("Process exitValue: " + exitVal);
} catch (Throwable t)
{
t.printStackTrace();
}
}
}
Now here is the sample program I am trying to control. (Keep in mind both of these I am using now are babies compared to the ones I am working on, but these are simple and small):
import java.io.*;
public class sampleTool {
public sampleTool() {
InputStreamReader inText = new InputStreamReader(System.in);
BufferedReader buffText = new BufferedReader(inText);
try {
System.out.println("This is a test of your stuff, enter your name: ");
String firstIn = buffText.readLine();
System.out.println("The name you entered was: " + firstIn);
}
catch(IOException err) {
System.out.println("Error reading line");
}
}
public static void main(String[] args) {
sampleTool tool = new sampleTool();
}
}
The problem lies in the fact that when run, the controller will display the first line of the sample program, but hangs up when it expects user input (on the sample program).
Anyone who does Java have any explaination?
Thx in advance!