Solved

How to run a process?

Posted on 1998-05-27
7
245 Views
Last Modified: 2010-04-16
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)?
0
Comment
Question by:msmolyak
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 6

Accepted Solution

by:
gadio earned 100 total points
ID: 1221533
msmolyak, I think that what you are missing is a close of the processInput at the end of the reading. probably the buffer didn't flushed to the new process when the feeding thread finished reading the input file. I have added a close and tried it (on UNIX machine) and it worked. Try it on the NT and tell me if it helped. Heres what I changed:

----------------

    try
    {
       while((nextLine = fileInput.readLine()) != null)
       {
           processInput.writeBytes(nextLine+"\n");
           System.out.println("Read input line: " + nextLine);
       }
       processInput.close();  // ---****< line added >*****---
    }
    catch(IOException e)
        {
           e.printStackTrace();
        }
    }

0
 
LVL 5

Author Comment

by:msmolyak
ID: 1221534
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.
0
 
LVL 6

Expert Comment

by:gadio
ID: 1221535
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.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Author Comment

by:msmolyak
ID: 1221536
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.
0
 
LVL 6

Expert Comment

by:gadio
ID: 1221537
I don't have an NT machine but I'll try it on win95, see how it goes.
0
 
LVL 5

Author Comment

by:msmolyak
ID: 1221538
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?
0
 
LVL 6

Expert Comment

by:gadio
ID: 1221539
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.
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.
Suggested Courses

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question