Java Program to Shutdown Computer in Linux

This is a Java Program to Shutdown or Turn Off the Computer in Linux.

We first check the name of the operating system of the computer. If its Linux we proceed else we would not. Now we would give number of seconds as input after which we want to turn off computer and then concatenate it with the shutdown command to turn off the computer.

Here is the source code of the Java Program to Shutdown or Turn Off the Computer in Linux. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. import java.io.IOException;
  2. import java.util.Scanner;
  3. public class Shutdown_System
  4. {
  5.     public static void main(String args[]) throws IOException
  6.     {
  7.         int sec;
  8.         String operatingSystem = System.getProperty("os.name");
  9.         System.out.println("Name of Operating System:"+operatingSystem);
  10.         if(operatingSystem.equals("Linux"))
  11.         {
  12.             Runtime runtime = Runtime.getRuntime();
  13.             Scanner s = new Scanner(System.in);
  14.             System.out.print("Enter the no. of seconds after which you want your computer to shutdown:");
  15.             sec = s.nextInt();
  16.             Process proc = runtime.exec("shutdown -h -t "+sec);
  17.             System.exit(0);
  18.         }
  19.         else
  20.         {
  21.             System.out.println("Unsupported operating system.");
  22.         }
  23.     }
  24. }

Output:

$ javac Shutdown_System.java
$ java Shutdown_System
 
Name of Operating System:Linux
Enter the no. of seconds after which you want your computer to shutdown:5

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.