Shut Down with Runtime class
Tags: beginners, java, discuss, codenewbie
Published: 2021-07-04 17:21:53

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](https://dev.to/ritvikdubey27/static-keyword-339n)***, since this is a static method we will call this method without creating an object of the class.

```Java
Runtime.getRuntime();
``` 
### 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.

```Java
Runtime.getRuntime().exec();
``` 
### 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. 

```Java
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");
	    }
    }
}
``` 
#### On compiling above code
![run.exec-1.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1625417438684/Nnj85pF7F.png)
#### On entering correct password
![run.exec-2.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1625417464531/muRoXrEbB.png)
#### On entering incorrect password 
![run.exec-3.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1625417498487/-_pAERo3S.png)
![run.exec-4.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1625417509145/PmHb4wJNv.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
```java
run.exec("shutdown -s -t");
``` 
- On replacing line number 14 with below code you can restart your pc
```java
run.exec("shutdown -r");
``` 
- On replacing line number 14 with below code you can restart your pc after specified time
```java
run.exec("shutdown -r -t");
``` 
- On replacing line number 14 with below code you can log out of your pc
```java
run.exec("shutdown -l");
``` 
- On replacing line number 14 with below code you can log out of your pc after specified time
```java
run.exec("shutdown -l -t");
``` 
- On replacing line number 14 with below code you can lock your pc
```java
run.exec("c:/windows/system32/rundll32.exe user32.dll, LockWorkStation");
``` 
### Apart from this you can run system applications(.exe) using it

```java
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();
        }
    }
}
``` 
### 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](https://twitter.com/rajatgour20)***
#### *Connect with me on [Twitter](https://twitter.com/ritvikdubey27?s=09) or [LinkedIn](https://www.linkedin.com/in/ritvik-dubey-3bbb22169)*
#### *My personal blog [blog.ritvikdubey.com](https://blog.ritvikdubey.com/)*