Java Runtime class

According to Sun Microsystems, Java monitors are reentrant means java thread can reuse the same monitor for different synchronized methods if method is called from the method.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.

The Runtime.getRuntime() method returns the singleton instance of Runtime class.

Important methods of Java Runtime class

SNo Method Description
1 public static Runtime getRuntime() returns the instance of Runtime class.
2 public void exit(int status) terminates the current virtual machine.
3 public void addShutdownHook(Thread hook) registers new hook thread.
4 public Process exec(String command)throws IOException executes given command in a separate process.
5 public int availableProcessors() returns no. of available processors.
6 public long freeMemory() returns amount of free memory in JVM.
7 public long totalMemory() returns amount of total memory in JVM.

Java Runtime exec() method

Flash Soon


How to shutdown system in Java

You can use shutdown -s command to shutdown system. For windows OS, you need to provide full path of shutdown command e.g. c:\\Windows\\System32\\shutdown.

Here you can use -s switch to shutdown system, -r switch to restart system and -t switch to specify time delay.

Flash Soon


How to shutdown windows system in Java

Flash Soon


How to restart system in Java

Flash Soon


Java Runtime availableProcessors()

Flash Soon


Java Runtime freeMemory() and totalMemory() method

Flash Soon



Copyright 2017 Design& Development