I'm executing the cmd process in Native windows from my Java program. I want to be able to view the input and the output of the forked process. However, I don't seem to be able to write anything to it from the Java program.
When I try to write a set of characters onto the command prompt in windows, I'm kind of expecting it to print something to the screen - anything. So, if I give it nonsensical garbage, then it should print that it doesn't understand that command.
However, after writing my garbage String onto the child process's OutputStream in the following code:
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("cmd");
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
PrintWriter out = new PrintWriter(process.getOutputStream());
String line = null;
out.println("My Garbage String");
while((line = readLine()) != null)
System.out.println(line);
the program stops where it says while((line = readLine()) != null)
.
So, the parent process is obviously blocking until the child process gets some input and unblocks and sends that input to the parent process. But, I'm giving the child process some input when I write anything out onto the PrintWriter
's println()
method, aren't I?
UPDATE
Now, because of some of the answers that were given to me, I decided to clarify what my problem is. I can read output from the command prompt fine. In fact, when I first run it, I get all the same ouput as you would normally get if you typed "cmd" in the command prompt.
For instance, take the following code example:
Process process = Runtime.getRuntime().exec("cmd");
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
PrintWriter out = new PrintWriter(process.getOutputStream());
String line = null;
do
{
line = in.readLine();
System.out.println(line);
}while(line!=null)
The output of the code will be the following:
Microsoft Windows XP [Version 5.1.2600] (C) Copyright 1985-2001 Microsoft Corp.
As you can see, the command prompt is giving my parent process some output. But, while running this in debug mode in my IDE, I find that it just simply stops at the line=in.readLine();
line. So, the child process blocks because there is nothing else to print.
So, when I try to give it some input using its OutputStream
property, it doesn't appear to respond. Instead, the program blocks at where line=in.readLine();
.
In response to what one poster suggested, the article you mentioned (When Runtime.exec() won't), it only goes over getting output from the OutputStream
and ErrorStream
properties. It does not mention how I can give it any kind of input other than the initial execution parameters that are given in the form of an array when you run Runtime.exec(args)
.
To simulate what I wanted to do initially (ie. give it random garbage and have it print that it doesn't understand the command), I could just simply type Runtime.getRumtime().exec("cmd /c random garbage);
in my code and run it. But, that isn't what I want to do. I want to communicate with the command prompt after it's already started its execution.
Ultimately, I want to be able to interact with other program that normally requires you to interact with them after execution from a Java program. Yet, if the only time that I can tell it what to do is when I specify its execution parameters, then I won't be able to interact with a separate process using Java in the way that I want to.
If anyone knows how this can be achieved, please tell me. Thanks.