#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    245
    Rep Power
    11

    Why doesn't start another process?


    I think this code should print the message more than once, but it only prints once. What's the problem? Thanks.

    Code:
    import java.io.IOException;
    
    public class RestartApplication {
    
    	public static void main(String[] args) {       
    		System.out.println("Test restarting the application!");       
    		restart();
    	}
    
    	private static void restart() {
    		try{
    			Runtime.getRuntime().exec("java RestartApplication");
    		}catch(IOException ie){
    
    		}
    		System.exit(0);
    	}   
    }
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2010
    Posts
    73
    Rep Power
    15
    Hi marlonmin,

    Your code may well be printing the message more than once, but the exec-ed call will be running in a different thread that will not have access to the console that outputs the first message; they will be totally separate processes.

    Note though that it is not just going to print the message out twice, it is going to keep running and running, because each call spawns a new process. I am going to reboot my PC now to kill off the dozens of threads that are taking up 100% of my CPU

    Hope this helps,
    slink

    Comments on this post

    • Lux Perpetua agrees : LOL! No, I would not advise running this code.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    San Francisco Bay
    Posts
    1,939
    Rep Power
    1313
    Originally Posted by marlonmin
    I think this code should print the message more than once, but it only prints once. What's the problem? Thanks.
    Your subprocess is piping its output back to the main process. It isn't printed automatically; you have to do it yourself. Here's an example. For obvious reasons, I didn't retain your infinite-respawn cycle. You may need to change the exec() command string to something appropriate for your system if you're not on Linux. (Anything that generates output without reading input will do.)
    java Code:
    import java.io.IOException;
     
    public class ProcessTest {
     
        public static void main(String[] args) {       
            System.out.println("Testing process creation...");       
            test_process();
        }
     
        private static void test_process() {
            Process p = null;
     
            try{
                /* Use a different command here if 'readlink' or
                 * '/dev/stdout' don't exist on your system.  This code is
                 * not meant to be portable.
                 */
                p = Runtime.getRuntime().exec("readlink -f /dev/stdout");
            } catch (IOException ie) {
                System.err.println("Couldn't run process.");
                System.exit(1);
            }
     
            int num_bytes = 0;
            byte [] b = new byte[4096];
     
            try {
                num_bytes = p.getInputStream().read(b);
            } catch (IOException ie) {
                System.err.println("Couldn't read pipe.");
                System.exit(1);
            }
     
            System.out.write(b, 0, num_bytes);
     
            System.exit(0);
        }   
    }

IMN logo majestic logo threadwatch logo seochat tools logo