0

Using the following code, I can get the .bat file to execute (no GUI, just the following lines). However, when I add it in as a method of the ActionListener for the button (its a Java Swing app), the .bat file never executes. Any ideas?

Runtime runtime = Runtime.getRuntime();
try {
    Process proc = runtime.exec("cmd /c start C:\\Users\\someName\\Desktop\\test.bat");
} catch (IOException e1) {
    e1.printStackTrace();
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 2
    Welcome to SO. Please read the [FAQ] and [Ask] for tips on writing good SO questions. How do you know it doesn't run? Have you verified that your code is being executed when the button is clicked? You have provided no information that anybody could use to help you. – Jim Garrison Nov 09 '12 at 05:11
  • see if this helps http://stackoverflow.com/questions/6735195/using-cmd-as-a-process?rq=1 – kosa Nov 09 '12 at 05:14
  • *"Any ideas?"* Tons, but with out context, none of which will be of any use to you. You might like to give us some more context and sample code of your actual problem – MadProgrammer Nov 09 '12 at 05:54
  • 2
    1) Go through the article linked from the [exec tag Wiki](http://stackoverflow.com/tags/runtime.exec/info) & implement all suggestions. That might lead to a solution to the immediate problem, but also.. 2) Use a `ProcessBuilder` that makes it easier to implement point (1) and.. 3) Provide arguments as an array. – Andrew Thompson Nov 09 '12 at 06:08
  • Do you know that the ActionListener is executed? – Ed Staub Nov 09 '12 at 15:28

1 Answers1

2

Me too getting the same errors.

You can use like this

public void actionPerformed(java.awt.event.ActionEvent evt) {

            File file = new File("filename.bat");
            try {
                Desktop.getDesktop().open(file);
                } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            jButton1ActionPerformed(evt);
        }
Murali
  • 774
  • 6
  • 12