Results 1 to 8 of 8
  1. #1
    itaipee is offline Member
    Join Date
    Dec 2008
    Posts
    99
    Rep Power
    0

    Default run command in the background ( useing runtime)

    I'm using runtime class for execute external commands

    I need to run command in the background .. like

    nedit a1.txt &

    but when I put ther & in the exe command

    Java Code:
      Process p = Runtime.getRuntime().exec("nedit a1.txt &");
    I get error messege that "&" could not be found...

  2. #2
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    When you use Runtime.exec, there is no shell interpreter involved. The "&" is a symbol interpreted by the shell. But you don't need it anyway, because all processes launched with Runtime.exec are placed in the background anyway.

  3. #3
    itaipee is offline Member
    Join Date
    Dec 2008
    Posts
    99
    Rep Power
    0

    Default

    First , well, I tried "tcsh nedit a1.txt &" - and yet, the & was treated like another file .
    Second - in any case , the job is run in the foreground, as my gui is being hold , and released itself only after I close the nedit .

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    You said that you UI is not responding at the file open? I'm not clear what you are saying, can you explain a bit.

  5. #5
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Quote Originally Posted by itaipee View Post
    First , well, I tried "tcsh nedit a1.txt &" - and yet, the & was treated like another file .
    Second - in any case , the job is run in the foreground, as my gui is being hold , and released itself only after I close the nedit .
    Post more code. What you're describing is not the default behavior of Runtime.exec().

  6. #6
    itaipee is offline Member
    Join Date
    Dec 2008
    Posts
    99
    Rep Power
    0

    Thumbs down

    very well

    First the listener:
    public class DoubleClickOnList extends MouseAdapter {
    public void mouseClicked(MouseEvent e) {
    if (e.getClickCount() == 2)
    { JList l1 = (JList) e.getComponent();
    int ind = l1.locationToIndex(e.getPoint());
    do_active(ind,l1);
    } } }
    and the function:

    Java Code:
    public void do_active(int ind, JList l1)
    	{
    	// l1: JList of files. that work fine
    		ViewFile f1=(ViewFile) l1.getModel().getElementAt(ind);
    // hh("viewer") is "nedit" or "gvim" - the problem is with both
    		String comm=seal.hh("viewer")+" "+f1.getFile().getPath()+"";
    		Seal.logger.info("exec :"+comm);
    		try {
    	
    		    Process p = Runtime.getRuntime().exec(comm);
    		    BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
    		    BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
    	   }  // end of try
    	    catch (IOException e) {		   
    	        exception=e;
        	System.out.println("exception happened - here's what I know: ");
            e.printStackTrace();
            return 0;
    	    }  // end of catch
    // by the way : there is no expectation for this command		 
    }

  7. #7
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Ok. Your problem is that you need different threads for reading the input and error streams. Of course you're not even using stdInput or stdError, so you could comment the following lines out and it will work just fine (unless the process you spawn is verbose and causes one of these buffers to overflow).

    Java Code:
    /*
    * You're not using these, and they are your problem
    *
    * BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream();
    * BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
    *
    */

  8. #8
    itaipee is offline Member
    Join Date
    Dec 2008
    Posts
    99
    Rep Power
    0

    Default

    Ok. Your problem is that you need different threads for reading the input and error streams. Of course you're not even using stdInput or stdError, so you could comment the following lines out and it will work just fine (unless the process you spawn is verbose and causes one of these buffers to overflow).
    Thanks , it solved my problem

Similar Threads

  1. How to Run a .exe in background???
    By jazz2k8 in forum New To Java
    Replies: 9
    Last Post: 01-21-2011, 11:27 AM
  2. Background in JFrame ( GUI).
    By Twister03 in forum AWT / Swing
    Replies: 2
    Last Post: 03-12-2009, 03:24 AM
  3. Replies: 6
    Last Post: 12-29-2008, 02:47 PM
  4. Image as background
    By Java.child in forum AWT / Swing
    Replies: 2
    Last Post: 10-02-2008, 11:37 PM
  5. Unable to execute command line command in java
    By LordSM in forum New To Java
    Replies: 1
    Last Post: 08-08-2007, 12:23 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
  •