tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Java > Runtime > Invoke External Applications

Invoke External Applications 

This tutorial shows how to list all system properties in one shot and execute external programs through java. We can use Runtime to execute any windows or Linux binary file. If your executable is in system PATH you can directly specify the executable name otherwise you need to specify the executable with full path. Still in doubt check the PATH environment variable. As notepad.exe is available in folder C:\WINDOWS\system32 which is already appended to system PATH we don't need to specify the full path.

File Name  :  
com/bethecoder/tutorials/runtime/RuntimeTest.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.runtime;

import java.io.IOException;

public class RuntimeTest {

  /**
   @param args
   */
  public static void main(String[] args) {

    //List system properties
    System.getProperties().list(System.out);
    
    //Executable in system PATH
    try {
      Runtime.getRuntime().exec("notepad.exe C:\\Realtek.log");
    catch (IOException e) {    
      e.printStackTrace();
    }
    
    //Executable not in system PATH
    try {
      Runtime.getRuntime().exec("F:\\SampleWindow.exe");
    catch (IOException e) {    
      e.printStackTrace();
    }
  }
}
   

It gives the following output,
java.runtime.name=Java(TM) SE Runtime Environment
sun.boot.library.path=C:\Program Files\Java\jre1.6.0_02\bin
java.vm.version=1.6.0_02-b05
java.vm.vendor=Sun Microsystems Inc.
java.vendor.url=http://java.sun.com/
path.separator=;
...
...
...
Along with the console output it opens the notepad and sample window as shown below,

runtime


 
  


  
bl  br