msmolyak
asked on
How to run a process?
I have an interactive command prompt-based program which I can execute at the command prompt (I am running on NT) by either typing a program name and interacting with the program or by putting my responses into a text file and typing at the prompt "my_prog input_file". The program runs reading the inputs and producing the outputs.
I am trying to run this program from within a Java application by using the code below:
import java.io.*;
public class OsUtil
{
public OsUtil()
{
}
public static void main(String[] args)
{
// Define a command
String commandName = "clsqry";
// Execute a command
try
{
Process process = Runtime.getRuntime().exec( commandNam e);
// Get the process input
DataOutputStream processInput = new DataOutputStream(process.g etOutputSt ream());
// Create a file input stream to read the program input
DataInputStream fileInput = new DataInputStream(new FileInputStream("input.txt "));
// Start the process feeder thread
ProcessFeeder feeder = new ProcessFeeder(fileInput, processInput);
feeder.start();
process.waitFor();
System.out.println(process .exitValue ());
process.destroy();
// Get the process output
BufferedReader out = new BufferedReader(new InputStreamReader(process. getInputSt ream()));
// Print the output
String nextLine;
while((nextLine = out.readLine()) != null)
{
System.out.println(nextLin e);
}
}
catch(IOException e)
{
e.printStackTrace();
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
// A separate thread to supply input to the process
class ProcessFeeder extends Thread
{
DataInputStream fileInput;
DataOutputStream processInput;
ProcessFeeder(DataInputStr eam in, DataOutputStream out)
{
this.fileInput = in;
this.processInput = out;
}
public void run()
{
// Read input from the file and send it to the process
String nextLine;
try
{
while((nextLine = fileInput.readLine()) != null)
{
processInput.writeChars(ne xtLine);
System.out.println("Read input line: " + nextLine);
}
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
File "input.txt" contains the user prompts. When I run this program it simply hangs without doing anything. I tried changing the command from simply "clsqry" to "clsqry < input.txt" (and commenting out the code which supplies input). I tried placing process.waitFor() call at the end of the program after the output has been read. Nothing helps.
So, what is the appropriate way to run an interactive command-line program on NT (I hope for a cross-platform solution but NT would be a good start)?
I am trying to run this program from within a Java application by using the code below:
import java.io.*;
public class OsUtil
{
public OsUtil()
{
}
public static void main(String[] args)
{
// Define a command
String commandName = "clsqry";
// Execute a command
try
{
Process process = Runtime.getRuntime().exec(
// Get the process input
DataOutputStream processInput = new DataOutputStream(process.g
// Create a file input stream to read the program input
DataInputStream fileInput = new DataInputStream(new FileInputStream("input.txt
// Start the process feeder thread
ProcessFeeder feeder = new ProcessFeeder(fileInput, processInput);
feeder.start();
process.waitFor();
System.out.println(process
process.destroy();
// Get the process output
BufferedReader out = new BufferedReader(new InputStreamReader(process.
// Print the output
String nextLine;
while((nextLine = out.readLine()) != null)
{
System.out.println(nextLin
}
}
catch(IOException e)
{
e.printStackTrace();
}
catch(InterruptedException
{
e.printStackTrace();
}
}
}
// A separate thread to supply input to the process
class ProcessFeeder extends Thread
{
DataInputStream fileInput;
DataOutputStream processInput;
ProcessFeeder(DataInputStr
{
this.fileInput = in;
this.processInput = out;
}
public void run()
{
// Read input from the file and send it to the process
String nextLine;
try
{
while((nextLine = fileInput.readLine()) != null)
{
processInput.writeChars(ne
System.out.println("Read input line: " + nextLine);
}
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
File "input.txt" contains the user prompts. When I run this program it simply hangs without doing anything. I tried changing the command from simply "clsqry" to "clsqry < input.txt" (and commenting out the code which supplies input). I tried placing process.waitFor() call at the end of the program after the output has been read. Nothing helps.
So, what is the appropriate way to run an interactive command-line program on NT (I hope for a cross-platform solution but NT would be a good start)?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
msmolyak, did you notice that I added at the end of each write a newline? It seems that without it the process got all the input as a single line. Also I changed the write from writeChars to writeBytes. writeChars didn't work when I tried it.
ASKER
Sorry, I did not mention it. Yes, I put those lines in place of mine. I even added a call to flush() after each write. Same effect.
I don't have an NT machine but I'll try it on win95, see how it goes.
ASKER
You know what. I placed call to waitFor() after the code which reads the output and everything worked. Is waitFor() supposed to be called before or after you read the output?
Oooh. That makes sense. This can also explain the fact that it worked on Unix and not on NT. Probably (I have to check this), the process on NT can't exit until it output buffers are cleared. On Unix I know that the process can exit at these conditions.
ASKER