Win a copy of Spring Boot in Practice this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

some ideas about control pc?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi ,everybody , i wanner write some codes in order to control pc , such as shutdown pc ,reboot pc . i don't like the way follow codes:
Runtime run = Runtime.getRuntime();
run.exec("sudo shutdown -h now")


have you any ideas?

whole thanks´╝ü
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I dont think there is another way to achieve this... anybody?
 
Saloon Keeper
Posts: 25842
184
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could write a JNI class that invokes the OS-level functions directly.

Although rebooting an OS from inside any application is a questionable practice. I'd generally put the application in a shell script and have it set an exit code that would then determine what, if any reboot-style action would be done by the next step in the shell script. That way I'd have an app that shutdown clean, and wouldn't have to worry about it maybe going crazy and rebooting unexpectedly due to a bug.
 
li yuanchun
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think that isn't safety , it can achieve that with shell only.
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A shellscript is an application, the shell by itself is an application, and the shutdown-applet provided by the desktop/the menu is an application too. I don't see much difference.
 
Tim Holloway
Saloon Keeper
Posts: 25842
184
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The difference is that the shell script is a text file. To monitor its behavior, you can simply list it. System-controlling shell scripts tend to be fairly simple and straightforward.

Applications, on the other hand, are binary "black boxes", and if the application's primary purpose is something other than simple system control, there's always a chance that there may be unforseen logic paths that can cause the system to shut down unexpectedly.

This isn't to say that shell scripts are absolutely safer, but they are more transparent.

The real reason why I'd prefer the shutdown to be done outside the app, however, is that that way the app will have been terminated (hopefully cleanly) before the shutdown process begins. So you won't have the danger of OS yanking resources out from under you while the app still has threads executing, buffers unflushed, network connections open, etc.
 
reply
    Bookmark Topic Watch Topic
  • New Topic