I am new to Java, I have built a GUI that contains a JButton. I would
like this JButton to execute a windows batch file when the user clicks
the button. Below is my attempt, please help.
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)
{
try {
Runtime rt = Runtime.getRuntime();
String[] args = {"c:\\test.bat"};
Process proc = rt.exec(args);
}
catch (IOException e) {
System.err.println (e);
}
}
> I am new to Java, I have built a GUI that contains a JButton. I would
> like this JButton to execute a windows batch file when the user clicks
[quoted text clipped - 13 lines]
>
> }
Maybe you should tell us what the problem is unless you think we can
read your mind.
At first glance, an ActionListener must implement "public void
actionPerformed(ActionEvent)" so you need to at least change that in
your code.
m.djukic@beotel.net - 20 May 2007 00:01 GMT
try:
...
Runtime r = Runtime.getRuntime();
Process p = null;
String cmd = "c://test.bat";
try {
p = r.exec(cmd);
p.waitFor(); //to wait to execute command
} catch (Exception e) {
System.out.println("error executing " + cmd);
}
System.out.println(cmd + " returned " + p.exitValue());