Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to run a process?

Posted on 1998-05-27
7
Medium Priority
?
258 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
  • 4
  • 3
7 Comments
 
LVL 6

Accepted Solution

by:
gadio earned 400 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

886 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