java program to shutdown computer




Asked on August 25, 2015
I want to shutdown my PC through a java program. Can any one tell how may I do this?


Replied on August 25, 2015
There are many ways to send a shutdown signal to the OS to shutdown PC in java. See the below code(java code) that send a signal of shutdown to OS.

package javaCodeTesting;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class PCShutdown {
public static void main(String[] args) throws IOException{
Runtime run = Runtime.getRuntime();
BufferedReader buffreader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the no. of seconds after which you want your computer to shutdown: ");
long a = Long.parseLong(buffreader.readLine());
Process pro = run.exec("shutdown -s -t " + a);
System.exit(0);
}

}



Write Answer










©2024 concretepage.com | Privacy Policy | Contact Us