Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 3rd, 2012, 12:30 PM
marlonmin marlonmin is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 244 marlonmin User rank is Private First Class (20 - 50 Reputation Level)marlonmin User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Day 17 h 29 m 59 sec
Reputation Power: 9
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);
	}   
}



Reply With Quote
  #2  
Old November 4th, 2012, 01:02 AM
slink's Avatar
slink slink is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2010
Posts: 73 slink User rank is Sergeant (500 - 2000 Reputation Level)slink User rank is Sergeant (500 - 2000 Reputation Level)slink User rank is Sergeant (500 - 2000 Reputation Level)slink User rank is Sergeant (500 - 2000 Reputation Level)slink User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Day 17 h 10 m 20 sec
Reputation Power: 14
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.

Reply With Quote
  #3  
Old November 4th, 2012, 03:40 AM
Lux Perpetua Lux Perpetua is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: San Francisco Bay
Posts: 1,938 Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 2 h 43 m 16 sec
Reputation Power: 1312
Quote:
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:
Original - java Code
  1. import java.io.IOException;
  2.  
  3. public class ProcessTest {
  4.  
  5.     public static void main(String[] args) {       
  6.         System.out.println("Testing process creation...");       
  7.         test_process();
  8.     }
  9.  
  10.     private static void test_process() {
  11.         Process p = null;
  12.  
  13.         try{
  14.             /* Use a different command here if 'readlink' or
  15.              * '/dev/stdout' don't exist on your system.  This code is
  16.              * not meant to be portable.
  17.              */
  18.             p = Runtime.getRuntime().exec("readlink -f /dev/stdout");
  19.         } catch (IOException ie) {
  20.             System.err.println("Couldn't run process.");
  21.             System.exit(1);
  22.         }
  23.  
  24.         int num_bytes = 0;
  25.         byte [] b = new byte[4096];
  26.  
  27.         try {
  28.             num_bytes = p.getInputStream().read(b);
  29.         } catch (IOException ie) {
  30.             System.err.println("Couldn't read pipe.");
  31.             System.exit(1);
  32.         }
  33.  
  34.         System.out.write(b, 0, num_bytes);
  35.    
  36.         System.exit(0);
  37.     }   
  38. }

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Why doesn't start another process?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap