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

Leave a Reply