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.
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
This article is contributed by Anannya Uberoi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Calling a method using null in Java
- Java.lang.Runtime class in Java
- Dynamic Method Dispatch or Runtime Polymorphism in Java
- How is CompileTime classpath different from RunTime classpath in Java?
- CompileTime Vs RunTime Resolution of Strings
- Java.lang.Process class in Java
- Pinging an IP address in Java | Set 2 (By creating sub-process)
- Process API Updates in Java
- Android Boot Process
- Program to add and Subtract Complex Numbers using Class in Java
- Java program to check palindrome (using library methods)
- Send email using Java Program
- Program to convert a Set to Stream in Java using Generics
- Java program to convert Currency using AWT
- Java program to store a Student Information in a File using AWT
- Hello World Program : First program while learning Programming
- Java Program to open the command prompt and insert commands
- Java program to List all files in a directory and nested sub-directories | Recursive approach
- Structure and Members of the Java Program
- Compilation and Execution of a Java Program
Improved By : DevParzival404