Java Program to Shut Down Computer in Linux
Last Updated :
08 Oct, 2021
In this program, we will shut down the Computer if the operating System is Linux. So, Firstly we will check that the System on which we are running Java Program is Linux or not. If the operating system is not Linux then we would not proceed further and print “Unsupported Operating System”. If it is a Linux operating system then the user will enter the time (in seconds) so that after that time PC would shut down.
Here time (in seconds) doesn’t include the time taken by Your System to shut down. For Example: if the user will input 1 sec then it consists of 1 second plus your system time required to shut down. This is because due to system architecture, availability of RAM, etc. affects the Shutting Down Speed.
Approach:
- Import Required Packages.
- Check your PC has Linux Operating System or Not.
- If it is not Linux then print “Unsupported Operating System”.
- IF it is Linux the take input about “After how many seconds the PC will shut Down” to the user.
- PC will shut down.
Below is the implementation of the above approach:
Java
import java.io.IOException;
import java.util.Scanner;
public class SystemShutDown {
public static void main(String args[])
throws IOException
{
int seconds;
String operatingSystem
= System.getProperty( "os.name" );
System.out.println( "Name of Operating System:"
+ operatingSystem);
if (operatingSystem.equals( "Linux" )) {
Runtime runTime = Runtime.getRuntime();
seconds = 5 ;
Process processing
= runTime.exec( "shutdown -h -t " + seconds);
System.exit( 0 );
}
|
Output:
Name of Operating System:Linux
//Shutting Down your Linux system
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...