Wednesday, July 3, 2024
HomeLanguagesJavaCalling an External Program in Java using Process and Runtime

Calling an External Program in Java using Process and Runtime

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:

  1. Process class
  2. 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.




// 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

This article is contributed by Anannya Uberoi. If you like Lazyroar and would like to contribute, you can also write an article using contribute.neveropen.co.za or mail your article to contribute@neveropen.co.za. See your article appearing on the Lazyroar 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.

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments