Pages

Friday, April 10, 2020

Java Program To Shutdown Computer in Windows

1. Introduction


In this tutorial, We'll learn how to shut down computers in java using a command "shutdown -s -t". It takes time when you want to do shutdown the computer.

2. Java Program To Shutdown Computer


The below program takes the number of seconds as input and will use the Runtime class to execute the windows command with the help of exec() method.


package com.java.w3schools.blog.java.program.to;

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

/**
 * 
 * Shutdown in java.
 * 
 * @author JavaProgramTo.com
 *
 */
public class ShutdownComputer {

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

  Scanner scanner = new Scanner(System.in);

  System.out.println("Enter the time that you want to shutdown after some amount of time : ");
  Double timeInSeconds = scanner.nextDouble();

  Runtime runtime = Runtime.getRuntime();
  Process p = runtime.exec("shutdown -s -t " + timeInSeconds);

  System.exit(1);
  

 }

}

3. Conclusion


In this article, We've seen how to shut down the computer in java with windows operating systems. The example is shown with the Runtime class exec() method.

GitHub Code

No comments:

Post a Comment

Please do not add any spam links in the comments section.