dcsimg
Conversation between Thread not right.
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Thursday, December 7, 2006 10:05 PM

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 =   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: Conversation between Thread not right.

Posted By:   WarnerJan_Veldhuis  
Posted On:   Friday, December 8, 2006 07:46 AM

Stop crossposting: Original here
About | Sitemap | Contact