Runtime.exec() - Does not produce output
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Alfred_Wong
Posted On:   Friday, February 7, 2003 08:22 AM

Hey, I've been trying to get my program to run an application that I downloaded off the net. It is a console based telnet application build for windows. Now, I've been trying to bascially automate some telnet stuff but it doesn't seem to work at the moment that is it doesn't give me output and it causes my computer to slow down. I've noticed that it does start the application which causes the slow down but I was wondering why it doesn't give me any output. Here is the code that I have import java.io.*;public class Main { public static void main(String[] args) { String command = new String[3]; command[0] = "cmd.exe"; command[1] = "/C"; command[2] = "D:\Temp\   More>>

Hey,
I've been trying to get my program to run an application that I downloaded off the net. It is a console based telnet application build for windows. Now, I've been trying to bascially automate some telnet stuff but it doesn't seem to work at the moment that is it doesn't give me output and it causes my computer to slow down. I've noticed that it does start the application which causes the slow down but I was wondering why it doesn't give me any output. Here is the code that I have

			
import java.io.*;public class Main
{
public static void main(String[] args)
{
String command = new String[3];
command[0] = "cmd.exe";
command[1] = "/C";
command[2] = "D:\Temp\telnet elnet.exe";
try
{
Process p = Runtime.getRuntime().exec(command);
BufferdReader stdin = new BufferedReader(new InputStreamReader (p.getInputStream()));
BufferedReader stderr = new BufferedRader(new InputStreamReader (p.getErrorStream()));
PrintStream stdout = new PrintStream(p.getOutputStream());
String line = "";
while((line = stderr.readLine()) != null)
{
System.out.println(line);
}
System.out.println();
while((line = stdin.readLine()) != null)
{
System.out.println(line);
}
p.destroy();
}
catch(Exception ex)
{
System.out.println("Exception: " + ex.getMessage());
System.exit(-1);
}
}
}


Could some please help me and trying to get this program to work? I have tried sending it strings ( not in this code) but I still get the same result. Note that when I start this program in the normal NT os it has output displayed before it requires Input, thus shouldn't it print that out first?



Thanks    <<Less

Re: Runtime.exec() - Does not produce output

Posted By:   Stephen_Ostermiller  
Posted On:   Monday, February 10, 2003 11:31 AM

If you are trying to automate telnet stuff then why are you running it in a cmd window? the command window would handle the stdin and stdout of the program. You probably want to handle that stuff yourself.

Process p = Runtime.getRuntime().exec("D:\Temp\telnet\telnet.exe");


Second, I wouldn't read from each of the streams until each is done in succession. If the stdin buffer fills up before the program completes, you will never get to it.


while((stderrline = stderr.readLine()) != null && (stdinline = stdin.readLine()) != null){
if (stderrline != null){
System.err.println(stderrline);
}
if (stdinline != null){
System.out.println(stdinline);
}
}


What you have labeled as stdin is really the stdout of the program and what you have labeled as stdout is really the stdin of the program. From your point of view, the input and output is reversed.

Re: Runtime.exec() - Does not produce output

Posted By:   James_Bayer  
Posted On:   Friday, February 7, 2003 09:07 AM

you are missing a slash on this line:

   command[2] = "D:\Temp\telnet	elnet.exe";



should be:
   command[2] = "D:\Temp\telnettelnet.exe";

i've noticed that the process execution monitoring in java is very sensitive. for example, it has trouble monitoring the output of any program started with spaces, such as c:\program files\blah.exe, and works better with c:\progra~1\blah.exe



i hope this solves your problem.
About | Sitemap | Contact