Week 23 — What is the Java's `Runtime` class and what does it do?

Question of the Week #23
What is the Java's Runtime class and what does it do?
5 Replies
Eric McIntyre
Eric McIntyre2y ago
The Runtime class in Java is an object that allows you to launch new processes, work with system resources and manage the execution process of a program. This class has methods to interact with the operating system, such as executing external commands, terminating the JVM process, and determining the maximum amount of memory available for your application. For example, you can use Runtime to retrieve the maximum amount of free memory that JVM can allocate, or to check the total amount of available memory at the moment. Here is a sample code using the Runtime class:
public class RuntimeDemo {
public static void main(String[] args) {
// Get the current runtime environment
Runtime runtime = Runtime.getRuntime();

try {
// Launch a new process and pass a command to it
Process process = runtime.exec("notepad.exe");

// Wait until the process terminates
process.waitFor();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class RuntimeDemo {
public static void main(String[] args) {
// Get the current runtime environment
Runtime runtime = Runtime.getRuntime();

try {
// Launch a new process and pass a command to it
Process process = runtime.exec("notepad.exe");

// Wait until the process terminates
process.waitFor();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Submission from ЯЖЕГУСЪ#7761
Eric McIntyre
Eric McIntyre2y ago
Each Java application has a single instance of the Runtime class. This allows the application to interact with the environment in which the application is running. The getRuntime method determines the current Runtime. An application cannot create its own instance of this class. For example, you can use the runtime class to read out the total and free memory: Runtime.getRuntime().totalMemory(); Runtime.getRuntime().freeMemory();
Eric McIntyre
Eric McIntyre2y ago
public class MemoryTest{
public static void main(String args[])throws Exception{
Runtime r=Runtime.getRuntime();
System.out.println("Total Memory: "+r.totalMemory());
System.out.println("Free Memory: "+r.freeMemory());
}
}
public class MemoryTest{
public static void main(String args[])throws Exception{
Runtime r=Runtime.getRuntime();
System.out.println("Total Memory: "+r.totalMemory());
System.out.println("Free Memory: "+r.freeMemory());
}
}
⭐ Submission from StormOfGalaxy#8898
Eric McIntyre
Eric McIntyre2y ago
The Runtime class provides util, that allows the developer to inspect the current running VM. Things like memory stats, the amount of cores of the cpu, etc are found there. Runtime itself is nonstatic - all the methods are bound to an instance. You can't just make a new instance of it tho, there's only one instance available, stored in Runtime.currentRuntime. You can obtain it via Runtime.getRuntime() There is one static method on it - Runtime.version(). It provides version information about the current VM, things like the build number and semver information. Other funnies of the runtime class include - exec(): executes a shell command - addShutdownHook(): Adds a thread that runs when the jvm shuts down - gc(): Runs the garbage collector - and more The class itself isn't that big, but it does provide some nice tools to manipulate and work with the jvm runtime.
⭐ Submission from 0x150#9699
dan1st
dan1st2y ago
The class java.lang.Runtime provides multiple methods for interacting with the JVM. One can get an instance of the Runtime class using Runtime.getRuntime(). For example, it is possible to use Runtime.availableProcessors in order to get the number of processors usable by the JVM. It is also possible to get information about memory usage using Runtime#freeMemory and Runtime#totalMemory or access the currently used Java version with Runtime#version.
Runtime runtime = Runtime.getRuntime();
System.out.printf("""
%d available processors
%d/%d bytes free memory
Running on Java %s
""",runtime.availableProcessors(),runtime.freeMemory(), runtime.totalMemory(),String.valueOf(runtime.version()));
Runtime runtime = Runtime.getRuntime();
System.out.printf("""
%d available processors
%d/%d bytes free memory
Running on Java %s
""",runtime.availableProcessors(),runtime.freeMemory(), runtime.totalMemory(),String.valueOf(runtime.version()));
Aside from obtaining system information, the Runtime class can also be used to execute system command using Runtime#exec:
Process p = Runtime.getRuntime().exec(new String[]{"nslookup", "dev.java"});
Process p = Runtime.getRuntime().exec(new String[]{"nslookup", "dev.java"});
Furthermore, it allows registering (and removing) shutdown hooks. Shutdown hooks are executed when the JVM shuts down gracefully.
Runtime.getRuntime().addShutdownHook(new Thread(()->{
System.out.println("JVM shuts down");
}));
Runtime.getRuntime().addShutdownHook(new Thread(()->{
System.out.println("JVM shuts down");
}));
Another method provided by the Runtime class is called halt. This method immediately stops the JVM. Shutdown hooks or similar are not executed when calling Runtime#halt.
Runtime.getRuntime().halt(123);
Runtime.getRuntime().halt(123);
(answer by me, wasn't reviewed/no point was granted - I'll just leave that here for archiving purposes)
Want results from more Discord servers?
Add your server