Java Program To Shutdown Windows Desktop

The execution of a computer program controls every digital device you use. Programs are a set of instructions given to a system for performing specific operations. Also, programs help the hardware and software to interact with each other. And with the help of these programs, you can also control your desktop environment. If you know basic programming, then giving instructions to your desktop with the use of pre-written programs might make you seem more extraordinary in front of your friends. So today, we’re going to start with a straightforward Java Program To Shutdown Windows Desktop.

 

What’s The Approach?

 

  • Firstly we are going to import all files from the java.io library.

 

  • Now, inside the main method, we will create a new object of the Runtime class, i.e., Runtime runtime 

 

  • After that, assign the newly created Runtime object, the value of Runtime.getRuntime();

 

  • Now inside the try block, execute the statement. runtime.exec("shutdown -s -t 5");

 

  • And inside the catch block, catch IOException

 

Also Read: Implement Radix Sort Using Java

 

Java Program To Shutdown Windows Desktop

 

Output:

Shutting down the PC after 5 seconds.

 

import java.io.*;

public class TechDecodeTutorials
{
   public static void main(String[] args)
   {
      Runtime runtime = Runtime.getRuntime();
      try
      {
         System.out.println("Shutting down the PC after 5 seconds.");
         runtime.exec("shutdown -s -t 5");
      }
      catch(IOException e)
      {
         System.out.println("Exception: " +e);
      }
   }
}

 

Ethix

I'm a coding geek interested in cyberspace who loves to write and read

Leave a Reply

Your email address will not be published. Required fields are marked *