Link to home
Start Free TrialLog in
Avatar of msmolyak
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(commandName);

            // Get the process input
            DataOutputStream processInput = new DataOutputStream(process.getOutputStream());

            // 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.getInputStream()));

            // Print the output
            String nextLine;
            while((nextLine = out.readLine()) != null)
            {
                System.out.println(nextLine);
            }
        }
        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(DataInputStream 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(nextLine);
                    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
Avatar of gadio
gadio

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of msmolyak
msmolyak

ASKER

I did close the process output stream (which accepts the input) as you suggested. Alas, it did not have any effect on the program. When I step throgh it it gets stuck on process.waitFor() call. Which tells me that it is sitting in memory waiting for inputs. Based on the printouts in the Thread class all the inputs were written and the stream closed.
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.
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.
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.