Java: Runtime Class


Every Java application has a single instance of class java.lang.Runtime to interact with java runtime environment(JRE) in which the application is running. This class provides a method to invoke GC, get total and free memory and execute a process, etc.

We can get application current runtime by getRuntime() method but we can’t create our own instance of Runtime.

Runtime runtime=Runtime.getRuntime()

This method return singleton instance of runtime.

Methods of Runtime Class

Modifier and Type Method  Description
void addShutdownHook(Thread hook) Registers a new shutdown hook for the virtual machines.
int availableProcessors() Returns the number of available processors for JVM.
Process exec(String command) Executes the given string command as a separate process.
void exit(int status) Terminates the currently running JVM by initiating its shutdown sequence.
long freeMemory() Returns the amount of free memory in the Java Virtual Machine.
void gc() Runs the garbage collector.
static Runtime getRuntime() Returns the runtime instance associated with the current application.
void halt(int status) Forcibly terminates the currently running JVM.
void load(String fileName) Loads the native library given by the fileName argument.
void loadLibrary(String libName) Loads the native library given as the libName argument.
long maxMemory() Returns the maximum amount of memory that the JVM will attempt to use.
boolean removeShutdownHook(Thread hook) De-registers a previously registered  shutdown hook for JVM(Java Virtual Machine).
void runFinalization() Runs the finalization methods of any objects pending finalization.
long totalMemory() Returns the total amount of memory in the JVM.
void traceInstructions(boolean on) Enables/Disables tracing of instructions.
void traceMethodCalls(boolean on) Enables/Disables tracing of method calls.

Java Runtime Class Example

In this example, you will see no of the available processors in JVM and differences in available memory after the execution of Garbage collection.

public class RuntimeClassExample {

	public static void main(String[] args) {
		  Runtime runtime =  Runtime.getRuntime();

		  //Total Memory
		  System.out.println("Total Memory in JVM: "+runtime.totalMemory());
		  //Return amount of memory available in JVM
	      System.out.println("Free memory in JVM before Garbage Collection = "+runtime.freeMemory());
	      //Call Garbage Collection
	      runtime.gc();
	      //Return amount of memory available in JVM
	      System.out.println("Free memory in JVM after Garbage Collection = "+runtime.freeMemory());

	      System.out.println("Available Processors:"+Runtime.getRuntime().availableProcessors());
	}
}

Output

Total Memory in JVM: 128974848
Free memory in JVM before Garbage Collection = 127611672
Free memory in JVM after Garbage Collection = 128005400
Available Processors:8

Java Runtime exec() method Example

Runtime.exec() method is used to execute commands that we generally run by command prompt.

How to open notepad by Java?

public class RuntimeExecExample{
 public static void main(String args[])throws Exception{
  //note pad command will open notepad
  Runtime.getRuntime().exec("notepad");
 }
}

How to shutdown system/window OS by Java?

You can use shutdown -s command to shutdown system. In the case of windows OS, you need to provide the full path of shutdown command i.e c:\\Windows\\System32\\shutdown.
Here are a couple of system arguments we can use:

  • -s switch to the shutdown system.
  • -r switch to restart the system.
  • -t switch to specify time delay.

Restart System

public class RuntimeExecExample{
 public static void main(String args[])throws Exception{
  //switch to shutdown system
  Runtime.getRuntime().exec("shutdown -s -t 0");
 }
}

Switch Window OS

public class RuntimeExecExample{
 public static void main(String args[])throws Exception{
  //switch to shutdown window OS
  Runtime.getRuntime().exec("c:\\Windows\\System32\\shutdown -s -t 0");
 }
}

How to restart system/window OS by Java?

Restart System

public class RuntimeExecExample{
 public static void main(String args[])throws Exception{
  //restart system
  Runtime.getRuntime().exec("shutdown -r -t 0");
 }
}

Restart Window OS

public class RuntimeExecExample{
 public static void main(String args[])throws Exception{
  //restart window OS
  Runtime.getRuntime().exec("c:\\Windows\\System32\\shutdown -r -t 0");
 }
}

References

https://docs.oracle.com/javase/8/docs/api/java/lang/Runtime.html

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s