dcsimg
It's all about Stream and Runtime
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Thursday, December 7, 2006 09:56 PM

How to get the right conversation between WindowsExec and maintest? I hope my question easy to understand. Thanks....    More>>

Hi' this is really makes me feel so upset. Why every time i try to execute WindowsExec file.
There's no error in code but the output not correctly.



WindowsExec.java :

			
public class WindowsExec
{
response std_out,std_err;
DataOutputStream doStream;
outputer std_in;

public WindowsExec()
{
try
{
String[] cmd = new String[]{"cmd.exe","/C","java","maintest"};
Runtime rt = Runtime.getRuntime();

Process proc = rt.exec(cmd);

std_out = new response(proc.getInputStream(),System.out);
std_err = new response(proc.getErrorStream(),System.err);

doStream = new DataOutputStream(proc.getOutputStream());
std_in = new outputer(doStream);

std_out.start();
std_err.start();
std_in.start();

int exitVal = proc.waitFor();
System.out.println("ExitValue: " + exitVal);

proc.destroy();
doStream.close();
}
catch (Throwable t)
{
t.printStackTrace();
}
}
public static void main(String args[])
{
new WindowsExec();
}
}



response.java :

			
class outputer extends Thread
{
OutputStream write;
BufferedReader bfr;
public outputer(OutputStream write)
{
try
{
this.write = write;

}
catch(Exception e)
{
e.printStackTrace();
}
}
public void run()
{
try
{
bfr = new BufferedReader(new InputStreamReader(System.in));
String input = null;

input = bfr.readLine();
write.write(input.getBytes());
write.flush();

write.close();
bfr.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}



maintest.java :

			
public class maintest
{

public maintest()
{
try
{
BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in));

System.out.print("Insert : ");
String getWord = bfr.readLine();

System.out.println(getWord);
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
new maintest();
}
}



This is the illustration :

1. Execute WindowsExec

2. Maintest executed from WindowsExec. (see WindowsExec source).

3. Display the output.



The output should be like this : (see maintest source to check)

1. Print Insert : and wait input from user.

2. User write some input and press enter.

3. Read input from user and display it.

4. Program terminate.



But what i got, the output are like this : (this is not correctly)

1. Wait input from user. (see maintest source,the firstline should be print "Insert :",not waiting some input).

2. User write some input and press enter

3. print Insert : and read input from user and display it.
4. Program terminate.



Why the output become like that? (Conversation between WindowsExec and maintest not correctly).

Insert : should appear in the first line of the output.



How to get the right conversation between WindowsExec and maintest?



I hope my question easy to understand.



Thanks....    <<Less

Re: It's all about Stream and Runtime

Posted By:   Stephen_Ostermiller  
Posted On:   Wednesday, December 13, 2006 03:18 PM

There are a couple of things that could be causing problems.


  1. Output to standard out may be buffered. If you want that output to be available to another program right away, call System.out.flush(). Specifically, you might want to try that right after printing "Insert" in maintest.

  2. On the other end, you appear to be reading lines via a buffered reader. The getLine() method of BufferedReader only returns full lines with a new line character. I think that what is happening is that the full line isn't available until after the user presses enter. You should either println your insert in maintest, or read character arrays rather than lines in your response class.

About | Sitemap | Contact