Calling an External Program in Java using Process and Runtime Last Updated : 09 Aug, 2019 Comments Improve Suggest changes 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 Calling an External Program in Java using Process and Runtime A Anannya Uberoi 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 Standard Practices for Protecting Sensitive Data in Java The Java platform provides an environment for executing code with different permission levels and a robust basis for secure systems through features such as memory safety. The Java language and virtual machine make application development highly resistant to common programming mistakes, stack smashi 4 min read Commonly Asked Java Programming Interview Questions | Set 2 In this article, some of the most important Java Interview Questions and Answers are discussed, to give you the cutting edge in your interviews. Java is one of the most popular and widely used programming language and platform. Java is fast, reliable and secure. From desktop to web applications, sci 10 min read Commonly Asked Java Programming Interview Questions | Set 1 Why is Java called the âPlatform Independent Programming Languageâ? Platform independence means that execution of your program does not dependent on type of operating system(it could be any : Linux, windows, Mac ..etc). So compile code only once and run it on any System (In C/C++, we need to compile 5 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 Simple Calculator using TCP in Java Prerequisite: Socket Programming in Java Networking just doesn't conclude with a one-way communication between the client and server. For example consider a time telling server which listens to request of the clients and respond with the current time to the client. Real-time applications usually fol 5 min read Like