Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / May 2007

Tip: Looking for answers? Try searching our database.

JButton and Execute Windows Batch File

Thread view: 
banker123 - 17 May 2007 18:14 GMT
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);
   }

   }
Brandon McCombs - 18 May 2007 04:13 GMT
> 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());


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.