Warning

 

Close

Confirm Action

Are you sure you wish to do this?

Confirm Cancel
Member Login
Site Notices
Posted: 9/14/2004 7:21:19 PM EST
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!
Link Posted: 9/14/2004 7:33:44 PM EST
It's probably simpler to just open up a socket on the loopback interface and send all your I/O over that. It's pretty straightforward.

From memory:

on the program to be controlled:

// You should limit this by binding only to 127.0.0.1
ServerSocket serverSock= new ServerSocket(4588); // arbitrary port no

Socket sock = serverSock.accept();

InputStream is = sock.getInputStream();

....yadda yadda

On the one doing the controlling:

InetAddress addr = InetAddress.getByName("127.0.0.0");
Socket sock = new Socket(addr, 4588);
OutputStream os = sock.getOutputStream();

..yadda yadda

Link Posted: 9/14/2004 7:50:39 PM EST
Well, ATM it isn't going to be going over a network. I just want it to be able to handle the I/O without locking up.
Link Posted: 9/14/2004 7:56:22 PM EST
I'd use queues for this (or sockets). Shared memory on NT/2k, or virtual queues on unix are easy to set up and use.
Link Posted: 9/14/2004 8:13:43 PM EST
Link Posted: 9/14/2004 9:51:54 PM EST
Does it print "</ERROR>" before hanging up?

I would normally use a pipe instead in stdin/stdout, but in either case the reader should probably execute in a separate thread so the primary thread can remain responsive.
Link Posted: 9/16/2004 7:16:14 AM EST
The loopback interface exists on any machine with a TCP/IP stack. It's a local psuedo-interface, no ethernet required, and you can't connect to it from outside.
Top Top