DEV Community

Cover image for Shut Down with Runtime class
Ritvik Dubey
Ritvik Dubey

Posted on

Shut Down with Runtime class

Hello All👋 I hope you all are doing well. In this very short article I'll be writing about how you can shut down your system using Runtime class in Java.

Let's begin...

What is Runtime class?

Java Runtime class is used to interact with java runtime environment. Java Runtime class provides methods to execute a process, invoke GC, get total and free memory etc. There is only one instance of java.lang.Runtime class is available for one java application.

Let's not get much into Runtime class and see how to shut down your pc.

For doing so we must know a few things first:-

getRuntime() method

This method returns the runtime object associated with the current Java application. This is a static method of Runtime class, to know more about static method read my last article static keyword in Java, since this is a static method we will call this method without creating an object of the class.

Runtime.getRuntime();
Enter fullscreen mode Exit fullscreen mode

exec() method

To execute a system command we pass the command string to the exec() method of the Runtime class. The exec() method returns a Process object that abstracts a separate process executing the command. From the Process object we can get outputs from and send inputs to the command. To use this method we will call it using method().method() syntax since both the methods are of same class.

Runtime.getRuntime().exec();
Enter fullscreen mode Exit fullscreen mode

Let's see code to shutdown your pc

To shut down we will use the command shutdown -s. We can also specify the time after which you want to make your computer shut down.

import java.lang.Runtime;
import java.util.Scanner;

public class ShutDown {
    public static void main(String[] args)throws Exception {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter Password");
        String pass = sc.next();
        if(pass.equals("xyz")) {
            System.out.println("Welcome, you entered correct password");
        }
        else {
            System.out.println("You entered wrong password, soon your machine will shut down automaticallly");
            run.exec("shutdown -s");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

On compiling above code

run.exec-1.png

On entering correct password

run.exec-2.png

On entering incorrect password

run.exec-3.png
run.exec-4.png

Several other tasks you can perform instead of shut down

  • On replacing line number 14 with below code you can shut down your pc after specified time
run.exec("shutdown -s -t");
Enter fullscreen mode Exit fullscreen mode
  • On replacing line number 14 with below code you can restart your pc
run.exec("shutdown -r");
Enter fullscreen mode Exit fullscreen mode
  • On replacing line number 14 with below code you can restart your pc after specified time
run.exec("shutdown -r -t");
Enter fullscreen mode Exit fullscreen mode
  • On replacing line number 14 with below code you can log out of your pc
run.exec("shutdown -l");
Enter fullscreen mode Exit fullscreen mode
  • On replacing line number 14 with below code you can log out of your pc after specified time
run.exec("shutdown -l -t");
Enter fullscreen mode Exit fullscreen mode
  • On replacing line number 14 with below code you can lock your pc
run.exec("c:/windows/system32/rundll32.exe user32.dll, LockWorkStation");
Enter fullscreen mode Exit fullscreen mode

Apart from this you can run system applications(.exe) using it

import java.io.IOException;

public class SystemApp {
    public static void main(String[] args) {
        try {
            Runtime run = Runtime.getRuntime();
            String path = "C:\\Users\\hp\\AppData\\Local\\Programs\\Microsoft VS Code\\Code.exe";
            Process process = run.exec(path);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Okay so that's enough for now follow my this journey to learn more about Java.

Thank you for reading.

Please share your thoughts about it and correct me if I'm wrong.

I hope you liked it and found it helpful.

Cover:- Rajat Gour

Connect with me on Twitter or LinkedIn

My personal blog blog.ritvikdubey.com

Oldest comments (0)