| View previous topic :: View next topic |
| Author |
Message |
banker123 Guest
|
Posted: Thu May 17, 2007 10:14 pm Post subject: JButton and Execute Windows Batch File |
|
|
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);
}
} |
|
| Back to top |
|
 |
Brandon McCombs Guest
|
Posted: Fri May 18, 2007 7:11 am Post subject: Re: JButton and Execute Windows Batch File |
|
|
banker123 wrote:
| Quote: | 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);
}
}
|
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. |
|
| Back to top |
|
 |
Guest
|
Posted: Sun May 20, 2007 4:01 am Post subject: Re: JButton and Execute Windows Batch File |
|
|
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()); |
|
| Back to top |
|
 |
|