Results 1 to 9 of 9
- 09-20-2011, 03:14 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 29
- Rep Power
- 0
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"); }
- 09-20-2011, 03:31 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Re: Execute .bat file when I press jMenuItem
What happens when you try that code?
- 09-20-2011, 03:35 PM #3
Member
- Join Date
- Dec 2009
- Posts
- 29
- Rep Power
- 0
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)
- 09-20-2011, 03:39 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Re: Execute .bat file when I press jMenuItem
That's because exec() throws an IOException, which you need to handle.
- 09-20-2011, 04:04 PM #5
Member
- Join Date
- Dec 2009
- Posts
- 29
- Rep Power
- 0
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(); }
- 09-20-2011, 04:08 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Re: Execute .bat file when I press jMenuItem
Does it work?
- 09-20-2011, 04:30 PM #7
Member
- Join Date
- Dec 2009
- Posts
- 29
- Rep Power
- 0
Re: Execute .bat file when I press jMenuItem
some kind of...
- 09-20-2011, 04:42 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Re: Execute .bat file when I press jMenuItem
Well, there you go.
- 09-20-2011, 08:12 PM #9
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
-
Execute .jar file within java program?
By JHumphr in forum Advanced JavaReplies: 2Last Post: 06-07-2011, 06:19 PM -
how to execute a jar file
By renu in forum New To JavaReplies: 2Last Post: 02-03-2011, 04:09 PM -
jar file won't execute.
By Splat in forum New To JavaReplies: 5Last Post: 10-22-2009, 05:26 AM -
Press any key to continue/press enter
By JT4NK3D in forum New To JavaReplies: 1Last Post: 11-17-2007, 09:27 PM -
Execute Jar file
By barney in forum New To JavaReplies: 1Last Post: 08-07-2007, 07:13 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks