import java.io.IOException; public class RuntimeExample { public static void main(String[] args) { try { // Create a new directory using the 'mkdir' command Process process = Runtime.getRuntime().exec("mkdir new_directory"); // Wait for the process to complete process.waitFor(); System.out.println("Directory created successfully!"); } catch (IOException | InterruptedException e) { e.printStackTrace(); } } }
Output:
Directory created successfully!
Developers can easily interact with the operating system and complete a variety of tasks by utilising the power of the RunTime class.
import java.io.IOException; public class RuntimeExample { public static void main(String[] args) { try { // Launch an external program using the 'notepad' command Process process = Runtime.getRuntime().exec("notepad"); // Wait for the process to complete process.waitFor(); System.out.println("External program executed successfully!"); } catch (IOException | InterruptedException e) { e.printStackTrace(); } } }
Output:
External program executed successfully!
In this example, the RunTime class is utilized to execute the "notepad" command, which launches the Notepad application. The waitFor() method ensures that the Java application waits until the external program completes its execution.
public class RuntimeExample { public static void main(String[] args) { // Get the Java runtime Runtime runtime = Runtime.getRuntime(); // Get the total memory available to the JVM in bytes long totalMemory = runtime.totalMemory(); System.out.println("Total memory: " + totalMemory + " bytes"); } }
Output:
Total memory: [totalMemory value] bytes
Developers can learn more about the runtime environment and make wise decisions based on the resources available by utilising the methods provided by the RunTime class.
Method | Description |
---|---|
public static Runtime getRuntime() | The Runtime class instance is returned by getRuntime(). |
public void addShutdownHook(Thread hook) | A new hook thread is registered by addShutdownHook(Thread hook). |
exec(String command) | It executes the specified command in a different process. |
public int availableProcessors() | availableProcessors() function returns the number of available processors. |
public long freeMemory() | The amount of JVM free memory is returned by freeMemory(). |
public long totalMemory() | Total JVM memory is returned by the function totalMemory(). |
public void exit(int status) | Starts the shutdown sequence for the currently running Java virtual machine. |
public class Main{ public static void main(String arg[]) { //getRuntime() Runtime runtime = Runtime.getRuntime(); System.out.println("Free Memory :" + runtime.freeMemory()); // totalMemory() System.out.println("Total Memory : "+runtime.totalMemory()); for(int i=0;i<1000;i++){ // Creating 1000 instance new Main(); } //freeMemory() System.out.println("Free Memory after creating 1000 instance : " + Runtime.getRuntime().freeMemory()); System.gc(); // The garbage collector is started. //This method's name implies that the Java virtual machine makes //greater efforts to recycle unused objects in order to make the //memory they currently occupy available for quick reuse. System.out.println("Free Memory after Running Garbage Collection : " + Runtime.getRuntime().freeMemory()); } }
Output:
Free Memory :199312320
Total Memory : 201326592
Free Memory after creating 1000 instance : 199312320
Free Memory after Running Garbage Collection : 7672824
public class Main{ public static void main(String arg[]){ // Creating a process and execute MS Word try { Process process = Runtime.getRuntime().exec("WINWORD"); // it will open ms word } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("MS Word Open successfully"); } }
Output:
MS Word Open successfully
public class Main{ public static void main(String arg[]){ //availableProcessors() method System.out.println("" + Runtime.getRuntime().availableProcessors()); //exit() method Runtime.getRuntime().exit(0); System.out.println("Program Running Check"); } }
Output:
4
Police Colony
Patna, Bihar
India
Email:
Post your comment