How to run command-line or execute external application from Java

How to run command-line or execute external application from Java

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

93 Comments

  1. Peter August 15, 2007
  2. Jennie September 22, 2007
  3. linglom September 24, 2007
  4. Mubarak November 29, 2007
  5. Mubarak November 29, 2007
  6. Mubarak November 29, 2007
  7. linglom December 1, 2007
  8. Raju February 7, 2008
  9. tilak February 8, 2008
  10. TrummA April 1, 2008
  11. Balu June 5, 2008
  12. Omega June 13, 2008
  13. Ringo June 14, 2008
  14. Dan June 18, 2008
  15. Thomas August 6, 2008
  16. linglom August 10, 2008
  17. John Ortega October 31, 2008
  18. John November 17, 2008
  19. linglom November 18, 2008
  20. John November 19, 2008
  21. linglom November 19, 2008
  22. John November 20, 2008
  23. linglom November 26, 2008
  24. John December 16, 2008
  25. Harris December 31, 2008
  26. linglom January 11, 2009
  27. Kevin Do March 9, 2009
  28. Kevin Do March 9, 2009
  29. linglom March 10, 2009
  30. Nikhil April 9, 2009
  31. linglom April 13, 2009
  32. Nikhil April 17, 2009
  33. linglom April 21, 2009
  34. amit May 9, 2009
  35. amit May 9, 2009
  36. linglom May 12, 2009
  37. [email protected]@S June 9, 2009
  38. PLK July 5, 2009
  39. Shailesh July 14, 2009
  40. Ashikur Rahman August 30, 2009
  41. linglom August 31, 2009
  42. sifa September 1, 2009
  43. linglom September 2, 2009
  44. Charles November 23, 2009
  45. tokared December 9, 2009
  46. Venkatesh December 21, 2009
  47. linglom January 6, 2010
  48. Martin H. Hamstad January 22, 2010
  49. Joe Negron NYC February 5, 2010
  50. aravin March 2, 2010
  51. linglom March 4, 2010
  52. Gabor March 5, 2010
  53. Gabor March 5, 2010
  54. arnie March 9, 2010
  55. Anup March 10, 2010
  56. linglom March 12, 2010
  57. arnie March 12, 2010
  58. Mohit March 31, 2010
  59. Saurabh April 7, 2010
  60. Dian April 22, 2010
  61. linglom April 25, 2010
  62. kanth April 29, 2010
  63. kiran May 21, 2010
  64. Isa August 27, 2010
  65. manu September 14, 2010
  66. Johndoe September 23, 2010
  67. Sridhar Narasimhan. October 25, 2010
  68. yo October 28, 2010
  69. Sameer November 23, 2010
  70. cruisel November 27, 2010
  71. Shafi December 2, 2010
  72. senioritta December 7, 2010
  73. senioritta December 7, 2010
  74. Jurgen January 3, 2011
  75. disky February 3, 2011
  76. bella February 10, 2011
  77. kevin March 14, 2011
  78. matt August 10, 2011
  79. Achilles September 2, 2011
  80. JavaGhost October 21, 2011
  81. vishwa January 3, 2012
  82. Sripal May 24, 2012
  83. JavaGhost June 22, 2012
  84. gayatri June 27, 2012
  85. Ajit August 22, 2012
  86. JavaGhost August 28, 2012
  87. saurabh September 3, 2012
  88. yo September 14, 2012
  89. Nadav January 17, 2013
  90. Hari February 15, 2014
  91. abdul June 6, 2015
  92. linglom June 6, 2015
  93. Neha Bansal December 23, 2016

Leave a Reply