my dog learned polymorphism*
The moose likes Java in General and the fly likes how to control native operating system operations with my java app Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to control native operating system operations with my java app" Watch "how to control native operating system operations with my java app" New topic
Author

how to control native operating system operations with my java app

Richards Milan
Greenhorn

Joined: Jun 24, 2012
Posts: 29

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


Do you know why Java programmers wear specs ? Its because they don't 'C#'
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18765
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Richards Milan
Greenhorn

Joined: Jun 24, 2012
Posts: 29

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

Joined: Jul 03, 2011
Posts: 692

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 ...


The Only way to learn is ...........do!
Visit my blog http://inaved-momin.blogspot.com/
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18765
    
  40

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

Joined: May 12, 2011
Posts: 36

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

Joined: Jul 03, 2011
Posts: 692

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

Joined: Mar 17, 2011
Posts: 7718
    
  20

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to control native operating system operations with my java app