How to execute a command using Runtime class in Java

Execute a command using Runtime class in Java

The java.lang.Runtime class is used to interact with the environment (Java virtual machine) in which the application is running. Java Runtime class provides methods to execute a command, invoke garbage collector, get total and free memory in the JVM etc. Every Java application has a single instance of Runtime class. In this post we will see some of the important methods in Runtime class and also how to execute a command using Runtime class in Java with an example program.

Important methods of Java Runtime class

Following are some of the important methods available in Runtime class.

getRuntime()Returns the runtime object associated with the current Java application. Most of the methods of class Runtime are instance methods and must be invoked with respect to the current runtime object.
exec(String command)Executes the specified string command in a separate process and returns a new Process object for managing the subprocess.
availableProcessors()Returns the number of processors available to the Java virtual machine.
freeMemory()Returns the amount of free memory (in bytes) in the JVM. Calling the gc method may result in increasing the value returned by freeMemory.
totalMemory()Returns the total amount of memory (in bytes) in the JVM. The value returned by this method may vary over time, depending on the host environment.
exit(int status)Terminates the currently running Java virtual machine by initiating its shutdown sequence.
gc()Runs the garbage collector. Calling this method suggests that the Java virtual machine spends effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse. When control returns from the method call, the virtual machine has made its best effort to recycle all discarded objects.
runFinalization()Runs the finalization methods of any objects pending finalization.
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.

Java program to execute external command using Runtime class

Here is a Java program to execute the command input by the user.

Here is a sample output of running the above program.

When you look at the above output we have given the input command as cmd /c start cmd.exe /K  cd c:/ & dir. The command simply says to open the windows command prompt and change the working directory to c: and also run the dir command. You can look at this link to learn about this command syntax.

After running the above program, a windows command prompt will open with working directory as c:/>. When you close the command prompt, dir command will be run in the current workspace location(classpath of the running Java project) and the output of the command will be collected by the BufferedReader and printed to the console. This output is what you see  after the “Command Output:” in the above example.

Here is another simple Java program to open notepad and close it after few seconds.

When you run the above program, you will see notepad getting opened and closed after 2 seconds.

Use of freeMemory(), totalMemory(), gc() methods in Runtime class

Here is the Java program showing usage of these methods.

Here is a sample output of running the above program.

Hope you have learnt how to execute an external command using Runtime class in Java. If you have any comments, post it in the comments section.

The following two tabs change content below.
Working as a Java developer since 2010. Passionate about programming in Java. I am a part time blogger.

Add Comment

Required fields are marked *. Your email address will not be published.