Topics Index

ProcessBuilderDemo - Java Example Program

class ProcessBuilderDemo
    public static void main(String arg[])
            ProcessBuilder proc = new ProcessBuilder("notepad.exe", "testfile");
        } catch (Exception e)
            System.out.println("Error Executing notepad.");

Error Executing notepad.


  • To create a process using ProcessBuilder , simply create an instance of ProcessBuilder specifying the name of the program and any needed arguments.
  • To begin execution of the program , call start() on that instance.
  • Here is an example that executes the windows text editor notepad. Notice that it specifies the name of the file to edit as an argument.

  • Basically if you execute this program in your corresponding desktops or in your laptops it will display a notepad window and with a file name testfile.
  • Otherwise an exception will rise as show in the program, here in this program first it tries to execute the try block but it failed and immediately it goes to the preceding one catch block, here catch block catches the exception and displays the exception.

© meritcampus 2019

All Rights Reserved.

Open In App