Have you ever confront a situation that you need to execute external programs while developing a Java application? For instance, you are developing a Java application and need to execute external application(another executable program) in the middle of the program or you may need to execute some commands such as listing directory command: dir (in windows) or ls (in Unix) while developing the program.

The following will show you how to execute the external program from Java application.

Example

Note: The example will use NetBeans as IDE.

Let see the example Java source code below:

 
import java.io.*;
 
public class Main {
 
       public static void main(String args[]) {
 
            try {
                Runtime rt = Runtime.getRuntime();
                //Process pr = rt.exec("cmd /c dir");
                Process pr = rt.exec("c:\\helloworld.exe");
 
                BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));
 
                String line=null;
 
                while((line=input.readLine()) != null) {
                    System.out.println(line);
                }
 
                int exitVal = pr.waitFor();
                System.out.println("Exited with error code "+exitVal);
 
            } catch(Exception e) {
                System.out.println(e.toString());
                e.printStackTrace();
            }
        }
}

The above Java’s code will try to execute the external program (helloworld.exe) and show output in console as exit code of the external program.

The sample external program, Helloworld.exe (Visual Basic)
Visual Basic - Hello World

Code Explanation:

Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("c:\\helloworld.exe");

Create Runtime Object and attach to system process. In this example, execute the helloworld.exe.
If you want to execute some commands, just modify the string in the rt.exec(“….�?) to command that you want.
For instance, in the comment line; //Process pr = rt.exec(”cmd /c dir”);
Note: that you need to enter “cmd /c …�? before type any command in Windows.

int exitVal = pr.waitFor();
System.out.println("Exited with error code "+exitVal);

Method waitFor() will make the current thread to wait until the external program finish and return the exit value to the waited thread.

Output example

Process pr = rt.exec(”c:\\helloworld.exe”);
Execute external program with exit code

Process pr = rt.exec(”cmd /c dir”);
Execute windows command

Summary

This is a simple code to execute external applications. If you want to call functions from other program (ex. C++, Visual Basic DLL), you need to use JNI (Java Native Interface) which you can find a nice tutorial at codeproject.com.
If you need more information, below are some sites that talk about executing external code.

For Java users

  • Execute an external program - Real’s Java How-to.
    http://www.rgagnon.com/javadetails/java-0014.html
  • When Runtime.exec() won’t - Java World.
    http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
  • Trouble with Runtime.getRuntime().exec(cmd) on Linux - Java.
    http://www.thescripts.com/forum/thread16788.html
  • Runtime.getRuntime().exec (Linux / UNIX forum at JavaRanch).
    http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=13&t=001755
  • Java’s Runtime.exec() and External Applications.
    http://www.ensta.fr/~diam/java/online/io/javazine.html

For C++ users

  • How can I start a process?
    http://www.codeguru.com/forum/showthread.php?t=302501
  • Executing programs with C(Linux).
    http://www.gidforums.com/t-3369.html
Share and Enjoy:
  • Digg
  • del.icio.us
  • Netvouz
  • description
  • ThisNext
  • MisterWong
  • Wists
  • BlinkList
  • Fark
  • Fleck
  • Furl
  • Reddit
  • Slashdot
  • SphereIt
  • StumbleUpon
  • Technorati
  • Blue Dot
  • description
  • MyShare
  • Spurl
  • YahooMyWeb

Related post