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.
import java.io.IOException;
import java.util.Scanner;
public class Shutdown_System
{
public static void main(String args[]) throws IOException
{
int sec;
String operatingSystem = System.getProperty("os.name");
System.out.println("Name of Operating System:"+operatingSystem);
if(operatingSystem.equals("Linux"))
{
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.exit(0);
}
else
{
System.out.println("Unsupported operating system.");
}
}
}
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
Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.
Related Posts:
- Check Java Books
- Apply for Computer Science Internship
- Practice Programming MCQs
- Check Programming Books
- Apply for Java Internship