how-to

Others

How to call external program/application

This sample shows how to call external program / application.

try {
  Process p = Runtime.getRuntime().exec( "notepad.exe" );

} catch (IOException ex) {
  Logger.getLogger(Window.class.getName()).log(Level.SEVERE, null, ex);
}

When you need do call with parameter:

try {
  Process p = Runtime.getRuntime().exec( "notepad.exe c:\\test.txt" );

} catch (IOException ex) {
  Logger.getLogger(Window.class.getName()).log(Level.SEVERE, null, ex);
}

Sample above call notepad and is trying to open specific text file.

When you use java.lang.Process.WaitFor() function, your application will be wait to the end of called program.

try {
  Process p = Runtime.getRuntime().exec( "notepad.exe c:\\test.txt" );

  try {
    /* -- wait for end of notepad.exe */
    p.waitFor();
  } catch (InterruptedException ex) {
    Logger.getLogger(Window.class.getName()).log(Level.SEVERE, null, ex);
  }

} catch (IOException ex) {
  Logger.getLogger(Window.class.getName()).log(Level.SEVERE, null, ex);
}