Results 1 to 9 of 9
  1. #1
    Radu is offline Member
    Join Date
    Dec 2009
    Posts
    29
    Rep Power
    0

    Default Execute .bat file when I press jMenuItem

    I searched the net but I didn't find a solution for this problem. How can I execute .bat file when I press jMenuItem in NetBeans.

    Java Code:
     private void ButonDownloadSActionPerformed(java.awt.event.ActionEvent evt) {
            //String TiparBat = "D:\\Radu\\Lucru\\Programare\\Test\\Tipar.bat";
            Runtime.getRuntime().exec("cmd /d start D:\\Radu\\Lucru\\Programare\\Test\\Tipar.bat");
    
        }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Execute .bat file when I press jMenuItem

    What happens when you try that code?

  3. #3
    Radu is offline Member
    Join Date
    Dec 2009
    Posts
    29
    Rep Power
    0

    Default Re: Execute .bat file when I press jMenuItem

    I get unreported exception java.io.IOException; must be caught or declared to be thrown
    ----
    (Alt-Enter shows hints)

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Execute .bat file when I press jMenuItem

    That's because exec() throws an IOException, which you need to handle.

  5. #5
    Radu is offline Member
    Join Date
    Dec 2009
    Posts
    29
    Rep Power
    0

    Default Re: Execute .bat file when I press jMenuItem

    Is this OK?

    Java Code:
    try {
          String TiparBat = "D:\\Radu\\Lucru\\Programare\\Test\\Tipar.bat";
          Process p = Runtime.getRuntime().exec("D:\\Radu\\Lucru\\Programare\\Test\\Tipar.bat");
          p.waitFor();
          System.out.println(p.exitValue());
        }
        catch (Exception err) {
          err.printStackTrace();
        }

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Execute .bat file when I press jMenuItem

    Does it work?

  7. #7
    Radu is offline Member
    Join Date
    Dec 2009
    Posts
    29
    Rep Power
    0

    Default Re: Execute .bat file when I press jMenuItem

    some kind of...

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Execute .bat file when I press jMenuItem

    Well, there you go.

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: Execute .bat file when I press jMenuItem

    It's generally recommended to catch the most specific exception, in this case IOException and not to use a "catch-all" for Exception. Also, if successful execution of the rest of the program depends on this code not throwing IOException, you should catch the IOException and throw an appropriate subclass of RuntimeException.

    db

Similar Threads

  1. Execute .jar file within java program?
    By JHumphr in forum Advanced Java
    Replies: 2
    Last Post: 06-07-2011, 07:19 PM
  2. how to execute a jar file
    By renu in forum New To Java
    Replies: 2
    Last Post: 02-03-2011, 05:09 PM
  3. jar file won't execute.
    By Splat in forum New To Java
    Replies: 5
    Last Post: 10-22-2009, 06:26 AM
  4. Press any key to continue/press enter
    By JT4NK3D in forum New To Java
    Replies: 1
    Last Post: 11-17-2007, 10:27 PM
  5. Execute Jar file
    By barney in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 08:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •