File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to control native operating system operations with my java app

 
Richards Milan
Greenhorn
Posts: 29
Android Java Mac
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to turn off my system by pressing a "Turn Off Computer" button in my java application. Can anyone help me in getting the code for that....?? I am using WINDOWS 7 HOME PREMIUM
 
Henry Wong
author
Marshal
Pie
Posts: 20820
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richards Milan wrote:I need to turn off my system by pressing a "Turn Off Computer" button in my java application. Can anyone help me in getting the code for that....?? I am using WINDOWS 7 HOME PREMIUM


Well, depending on what is installed, and your permissions, you may be able to do a system exec to the windows "shutdown" command.

Henry
 
Richards Milan
Greenhorn
Posts: 29
Android Java Mac
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, depending on what is installed, and your permissions, you may be able to do a system exec to the windows "shutdown" command.
can you elaborate it .. because I want to create an application using java that checks the password and shuts the system down when the user types in the wrong password !
any help with the code>>
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richards Milan wrote: Because I want to create an application using java that checks the password and shuts the system down when the user types in the wrong password !
any help with the code>>

No, you can't do this in pure Java , because the JVM itself gets started when user successfully logged-in , and in your case No JVM means No Java app can run as simple is that ...
 
Henry Wong
author
Marshal
Pie
Posts: 20820
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richards Milan wrote:can you elaborate it .. because I want to create an application using java that checks the password and shuts the system down when the user types in the wrong password !
any help with the code>>



To elaborate ....

1. Take a look at the javadoc for the java.lang.Runtime and java.lang.Process classes.
2. Take a look at this article about using those classes -- http://www.javaworld.com/jw-12-2000/jw-1229-traps.html
3. Take a look at the windows command that you want to execute -- http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/shutdown.mspx?mfr=true


Henry
 
Ankush Kaundal
Ranch Hand
Posts: 36
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richards Milan wrote:I need to turn off my system by pressing a "Turn Off Computer" button in my java application. Can anyone help me in getting the code for that....?? I am using WINDOWS 7 HOME PREMIUM


Well you can try doing this by getting reference to the runtime object associated with your java application (use static method getRuntime() of Runtime class for the same) and then pass on the String array mentioning the shutdown command and the required arguments to the exec(String[] cmdarray) method of the Runtime class. I think it will work then.
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankush Kaundal wrote:
Well you can try doing this by getting reference to the runtime object associated with your java application (use static method getRuntime() of Runtime class for the same) and then pass on the String array mentioning the shutdown command and the required arguments to the exec(String[] cmdarray) method of the Runtime class. I think it will work then.

He is not trying to close the windows after user successfully logged in ...actually he wants windows to shutdown when user enter wrong password at the log-in screen of windows

 
Winston Gutkowski
Bartender
Pie
Posts: 9442
49
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richards Milan wrote:I need to turn off my system by pressing a "Turn Off Computer" button in my java application. Can anyone help me in getting the code for that....?? I am using WINDOWS 7 HOME PREMIUM

Personally: I wouldn't try. Java is not the OS; and furthermore it's supposed to be platform-independent.

If it was me, I'd have my Java app return a code (say 98 - good old COBOL return code) when it exits to indicate that the user requested a shutdown, and then have the script or .BAT that runs the app do the shutdown for me.

Winston
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richards Milan wrote:I need to turn off my system by pressing a "Turn Off Computer" button in my java application. Can anyone help me in getting the code for that....?? I am using WINDOWS 7 HOME PREMIUM

Ohh so you have "turn off button" in your Java app then offcourse this is possible write a shutdown script for windows use *.bat or in *nix systems use *.sh file which contains command to shutdown system and run that file using Runtime or ProcessBuilder class
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic