Calling an External Program in Java using Process and Runtime Last Updated : 09 Aug, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report Java contains the functionality of initiating an external process - an executable file or an existing application on the system, such as Google Chrome or the Media Player- by simple Java code. One way is to use following two classes for the purpose: Process class Runtime class The Process class present in the java.lang package contains many useful methods such as killing a subprocess, making a thread wait for some time, returning the I/O stream of the subprocess etc. Subsequently, the Runtime class provides a portal to interact with the Java runtime environment. It contains methods to execute a process, give the number of available processors, display the free memory in the JVM, among others. Java // A sample Java program (Written for Windows OS) // to demonstrate creation of external process // using Runtime and Process class CoolStuff { public static void main(String[] args) { try { // Command to create an external process String command = "C:\Program Files (x86)"+ "\Google\Chrome\Application\chrome.exe"; // Running the above command Runtime run = Runtime.getRuntime(); Process proc = run.exec(command); } catch (IOException e) { e.printStackTrace(); } } } Runtime.getRuntime() simply returns the Runtime object associated with the current Java application. The executable path is specified in the process exec(String path) method. We also have an IOException try-catch block to handle the case where the file to be executed is not found. On running the code, an instance of Google Chrome opens up on the computer. Another way to create an external process is using ProcessBuilder which has been discussed in below post.ProcessBuilder in Java to create a basic online Judge Comment More infoAdvertise with us Next Article Process API Updates in Java K kartik Improve Article Tags : Java Java-Library Practice Tags : Java Similar Reads Compilation and Execution of a Java Program Java, being a platform-independent programming language, doesn't work on the one-step compilation. Instead, it involves a two-step execution, first through an OS-independent compiler; and second, in a virtual machine (JVM) which is custom-built for every operating system. The two principal stages ar 5 min read Java Program to Open the Command Prompt and Insert Commands In Java, the Runtime class allows Java applications to interact with the system's environment. Every Java application has an object of this class, this class provides exec() method and this method is used to run system commands.In this article, we will discuss how the exec() method can be used to op 2 min read Runnable, Callable, Future, Executor in Java & Android Multithreaded Programming Multithreading is a Java feature that allows concurrent execution of two or more parts of a program for maximum utilization of CPU. Each part of such a program is called a thread. So, threads are lightweight processes within a process. Runnable Any class whose instances are intended to be executed b 6 min read Process API Updates in Java By Process API, we can perform any operation related to a process.Suppose if we want the process id of current running process, or we want to create a new process, or want to destroy already running process, or want to find the child and parent processes of current running process, or if we want to 3 min read Introduction to Java Agent Programming Java agents are a part of the Instrumentation API in Java, which allows developers to modify the behavior of a running application by altering its bytecode. This process, known as instrumentation, does not require changes to the source code. Java agents are a powerful feature that can be used for pe 7 min read Pinging an IP address in Java | Set 2 (By creating sub-process) In article Pinging an IP address in Java, we have discussed how to ping an IP address using java.net.InetAddress.isReachable() method. In this post we will discuss how to execute ping command by creating a sub-process. Prerequisite : ProcessBuilder class , Process classBelow Java program creates a m 3 min read Like