Turn off PC with Java Program

Through run-time class we can interact with the system in which Java application is running. In this program we warn user with Popup messages and after this asked user to enter time in seconds to turn off the system. Then pass this user value to runtime function as a arguments and your work is done :) 

Source Code


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

public class Turn_Off_Pc{

public static void main(String[] args) throws IOException {

Runtime runtime = Runtime.getRuntime();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println(" \t \t \t Warning ! Save Your Work Before Run the Code");
System.out.println("Enter the Seconds to Turn off System :");
long seconds =Long.parseLong(br.readLine());
Process proc = runtime.exec("shutdown -s -t "+seconds);
System.exit(0);
}    }

Output of the Program

Turn off PC with Java Program
Turn off PC with Java Program

Share on Google Plus

About Asad

Asad Niazi is Software Engineer , Programmer, Web Developer and a young mentor of BloggersTown and PProgramming. Asad Love to writes about Technology, Programming, Blogging and make money online.

2 comments:

  1. Is this working on Linux and Mac Operating System .. ???

    ReplyDelete