Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2010
    Posts
    2
    Rep Power
    0

    Default 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.

  2. #2
    Join Date
    Jan 2010
    Posts
    2
    Rep Power
    0

    Default

    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

  1. problem with Runtime.getRuntime().exec when running java in .bat
    By Shayko in forum Threads and Synchronization
    Replies: 2
    Last Post: 01-27-2010, 07:46 PM
  2. Problem with Runtime.getRuntime().exec with Linux Commands
    By swapnilnawale in forum Threads and Synchronization
    Replies: 1
    Last Post: 09-23-2009, 10:23 PM
  3. Replies: 2
    Last Post: 01-06-2009, 03:53 PM
  4. runtime.exec stale process
    By karine in forum Advanced Java
    Replies: 6
    Last Post: 10-06-2008, 05:40 PM
  5. Problem with Runtime.exec()
    By nhabibi in forum Advanced Java
    Replies: 11
    Last Post: 07-02-2008, 01:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •