Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

shutdown pc by java without using command  RSS feed

 
tieu phung
Greenhorn
Posts: 4
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to make a program shutdown pc after a mount of times by java that no using command. It also consist of restart, hibernate,sleep, logoff...
i also want it to be platform independent
Could anyone help me?
 
Stephan van Hulst
Saloon Keeper
Posts: 7806
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't do this in a platform independent way, if at all.

Welcome to CodeRanch!
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I would do is create an "exec" command, like this:

Then I would test System.getProperty("os.name") for know OS names, create the OS shutdown command string and pass it to the exec above.
 
tieu phung
Greenhorn
Posts: 4
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi! thank for your answer. Is there another way??
 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To repeat what others have said:

1. No you can't do this in a platform independent way.
2. You can issue a OS specific command using exec(). For windows system see: https://technet.microsoft.com/en-gb/library/bb491003.aspx for details on the shutdown command.

If you need to be able to run your software on different platforms you could use the system properties to determine which OS the code is currently running on and issue an appropriate command for that OS.
 
Tim Holloway
Bartender
Posts: 18705
71
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note: It is very unlikely in order to shut down your computer this way that the application must be running as a privileged user. That means that anyone who has access to the app can do very bad things. As if unanticipated shutdowns weren't bad enough.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!