Java.lang.Runtime class in Java
Last Updated :
06 May, 2022
In Java, the Runtime class is used to interact with Every Java application that has a single instance of class Runtime that allows the application to interface with the environment in which the application is running. The current runtime can be obtained from the getRuntime() method.
Methods of Java Runtime class
Method |
Action Performed |
addShutdownHook(Thread hook) |
Registers a new virtual-machine shutdown hook thread. |
availableProcessors() |
Returns the number of processors available to the JVM (Java virtual machine) |
exec(String command) |
Executes the given command in a separate process |
exec(String[] cmd) |
Executes the specified command and arguments in a separate process. |
exec(String command, String[] envp, File dir) |
Executes the specified string command in a separate process with the specified environment and working directory. |
exec(String command, String[] envp) |
Executes the specified string command in a separate process with the specified environment. |
exec(String[] cmdarray, String[] envp, File dir) |
Executes the specified command and arguments in a separate process with the specified environment and working directory. |
exec(String[] cmdarray, String[] envp) |
Executes the specified command and arguments in a separate process with the specified environment. |
exit(int status) |
Terminates the currently running Java virtual machine by initiating its shutdown sequence. |
freeMemory() |
Returns the amount of free memory in the JVM(Java Virtual Machine) |
gc() |
Runs the garbage collector. Calling this method suggests that the Java virtual machine expands effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse. |
getRuntime() |
Returns the instance or Runtime object associated with the current Java application |
halt(int status) |
Forcibly terminates the currently running Java virtual machine. This method never returns normally. This method should be used with extreme caution. |
load(String filename) |
Loads the specified filename as a dynamic library. The filename argument must be a complete pathname. |
loadLibrary(String libname) |
Loads the dynamic library with the specified library name. A file containing code is loaded from the local system from a place where library files are conventionally obtained. |
maxMemory() |
Returns the maximum amount of memory that the Java virtual machine will attempt to use. If there is no inherent limit then the value Long.MAX_VALUE will be returned |
removeShutdownHook(Thread hook) |
De-registers a previously-registered virtual machine shutdown hook. |
runFinalization() |
Runs the finalization methods of any objects pending finalization. It suggests that JVM (Java virtual machine) expands effort toward running the finalize methods of objects that have been found to be discarded but whose finalize methods have not yet been run. |
totalMemory() |
Returns the amount of total memory in the JVM(Java Virtual Machine) |
traceInstructions(boolean a) |
Enables or disables tracing of instructions. If the boolean argument is true then it will suggest that the JVM emits debugging information for each instruction in the virtual machine as it is executed. |
traceMethodCalls(boolean a) |
Enables or disables tracing of method calls. If the boolean argument is true then it will suggest that the Java virtual machine emits debugging information for each method in the virtual machine as it is called. |
Example 1:
Java
import java.lang.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
Runtime run = Runtime.getRuntime();
System.out.println( "" + run.freeMemory());
System.out.println(
"" + Runtime.getRuntime().freeMemory());
System.out.println(
"" + Runtime.getRuntime().totalMemory());
}
}
|
Output
132579840
132285936
134217728
Example 2:
Java
import java.util.*;
import java.lang.*;
public class GFG {
public static void main(String[] args)
{
try {
Process process = Runtime.getRuntime().exec(
"google-chrome" );
System.out.println(
"Google Chrome successfully started" );
}
catch (Exception e) {
e.printStackTrace();
}
}
}
|
Output:
Google Chrome successfully started
Note: Replace with any software you want to start. Here we work on Linux and google-chrome is written like this way only. May differ in windows/mac.
Example 3:
Java
import java.util.*;
import java.lang.*;
public class GFG {
public static void main(String[] args) {
System.out.println( "" + Runtime.getRuntime()
.availableProcessors());
Runtime.getRuntime().exit( 0 );
System.out.println( "Program Running Check" );
}
}
|
From the above output, it is made clear that exit() method does not let below print statement to execute as “Program Running Check” is not printed on the console. It can be made clear if we comment out the working of availableProcessors() than exit() method output is as follows:
Example 4:
Java
public class GFG {
public static void main(String[] args)
{
Runtime.getRuntime().loadLibrary(
"/home/saket/Desktop/Library" );
System.out.println( "Library Loaded Successfully" );
Runtime.getRuntime().runFinalization();
System.out.println( "Finalized" );
Runtime.getRuntime().gc();
System.out.println( "Running" );
System.out.println(
"" + Runtime.getRuntime().maxMemory());
}
}
|
Output:
Library Loaded Successfully
Finalized
Running
2147483648
Example 5:
Java
public class GFG
{
public static void main(String[] args)
{
Runtime.getRuntime().halt( 0 );
System.out.println( "Process is still running." );
}
}
|
Output:
From above output it is made clear above program compiled successfully and run. There is no print statement is execute as we have used halt() method which terminates the further execution of operations.
Example 6:
Java
import java.io.*;
import java.util.*;
public class GFG {
public static void main(String[] args) {
try {
String[] cmd = new String[ 2 ];
cmd[ 0 ] = "atom" ;
cmd[ 1 ] = "File.java" ;
File dir = new File( "/Users/mayanksolanki/Desktop" );
Process process = Runtime.getRuntime().exec(cmd, null );
System.out.println( "File.java opening." );
}
catch (Exception e) {
e.printStackTrace();
}
}
}
|
Output:
File.java opening.