Results 1 to 2 of 2
- 01-21-2010, 10:47 AM #1
Member
- Join Date
- Jan 2010
- Posts
- 2
- Rep Power
- 0
problem with exec(), subprocess starts after main process
Hi all,
I have a problem. I must create a subprocess and connect to it with socket.
My process:
0. String command_to_exec_subprocess="java -cp"+cp+" mypackage.MyMainClass "+args;
1. Runtime.getRuntime().exec(command_to_exec_subproce ss);
2. socket.connect(new InetSocketAddress("localhost",25000), 3000);
My subprocess:
1. ServerSocket server=new ServerSocket(25000);
JFrame jf=new JFrame();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
2. Socket client=server.accept();
I use: Windows 7
JVM: 1.6
Problem is that frame popsup after my process finished, socket can never connect to server socket. As far as I know this means that Runtime waits with starting subprocess till my main process ends... Does any one know how to make the process execute just after I use exec command?? I tryed to use ProcessBuilder but when I use it it doesnt even start the subprocess... When i run the command from cmd on windows7 it runs with no problem.
Thx.
- 01-21-2010, 11:28 AM #2
Member
- Join Date
- Jan 2010
- Posts
- 2
- Rep Power
- 0
I found solution here:
When Runtime.exec() won't - JavaWorld
To try the connection I used:
new Thread(){
private BufferedReader is=new BufferedReader(new InputStreamReader(p.getInputStream()));
public void run(){
try {
String msg="";
while((msg=is.readLine())!=null){
System.out.println("STD IN:"+msg);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
new Thread(){
private BufferedReader is=new BufferedReader(new InputStreamReader(p.getErrorStream()));
public void run(){
try {
String msg="";
while((msg=is.readLine())!=null){
System.out.println("STD ERR:"+msg);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
its not very elegant but good for now to try if solution works.
Similar Threads
-
problem with Runtime.getRuntime().exec when running java in .bat
By Shayko in forum Threads and SynchronizationReplies: 2Last Post: 01-27-2010, 07:46 PM -
Problem with Runtime.getRuntime().exec with Linux Commands
By swapnilnawale in forum Threads and SynchronizationReplies: 1Last Post: 09-23-2009, 10:23 PM -
Java subprocesses via Runtime.exec() and windows "end process tree"...
By fxRichard in forum Advanced JavaReplies: 2Last Post: 01-06-2009, 03:53 PM -
runtime.exec stale process
By karine in forum Advanced JavaReplies: 6Last Post: 10-06-2008, 05:40 PM -
Problem with Runtime.exec()
By nhabibi in forum Advanced JavaReplies: 11Last Post: 07-02-2008, 01:35 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks