Tuesday, November 19, 2024
Google search engine
HomeLanguagesJavaJava Program to Shut Down Computer in Linux

Java Program to Shut Down Computer in Linux

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:

  1. Import Required Packages.
  2. Check your PC has Linux Operating System or Not.
  3. If it is not Linux then print “Unsupported Operating System”.
  4. IF it is Linux the take input about “After how many seconds the PC will shut Down” to the user.
  5. PC will shut down.

Below is the implementation of the above approach:

Java




// Java Program to Shut Down Computer
// when Operating System is Linux.
 
import java.io.IOException;
import java.util.Scanner;
 
public class SystemShutDown {
 
    public static void main(String args[])
        throws IOException
    {
        // Initialized to  take input from user in seconds
        int seconds;
 
        // Find Out Operating System
        String operatingSystem
            = System.getProperty("os.name");
 
        // Print Operating System of Computer
        System.out.println("Name of Operating System:"
                           + operatingSystem);
 
        // Check if Computer Operating System is Linux or
        // not
        if (operatingSystem.equals("Linux")) {
           
            // Returns the runtime object associated with
            // the current Java application.
            Runtime runTime = Runtime.getRuntime();
 
            // Take input from user in seconds
            seconds = 5;
 
            // Retrieve current Runtime Environment
            Process processing
                = runTime.exec("shutdown -h -t " + seconds);
 
            // Shutting Down the System
            System.exit(0);
        }


Output:

Name of Operating System:Linux  
//Shutting Down your Linux system

RELATED ARTICLES

Most Popular

Recent Comments