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.
public class Shutdown_System
public static void main(String args) throws IOException
String operatingSystem = System.getProperty("os.name");
System.out.println("Name of Operating System:"+operatingSystem);
Runtime runtime = Runtime.getRuntime();
Scanner s = new Scanner(System.in);
System.out.print("Enter the no. of seconds after which you want your computer to shutdown:");
sec = s.nextInt();
Process proc = runtime.exec("shutdown -h -t "+sec);
System.out.println("Unsupported operating system.");
$ 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.