AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

JButton and Execute Windows Batch File

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java GUI Toolkits
View previous topic :: View next topic  
Author Message
banker123
Guest





PostPosted: Thu May 17, 2007 10:14 pm    Post subject: JButton and Execute Windows Batch File Reply with 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);
}

}
Back to top
Brandon McCombs
Guest





PostPosted: Fri May 18, 2007 7:11 am    Post subject: Re: JButton and Execute Windows Batch File Reply with quote



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






PostPosted: Sun May 20, 2007 4:01 am    Post subject: Re: JButton and Execute Windows Batch File Reply with quote



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
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java GUI Toolkits All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.