I have created a bat file to call a java class. Now I have created a GUI in swing. In that swing I have a button as start and for that I have action Listener in which I created the following code
public void actionPerformed(java.awt.event.ActionEvent evt)
{
try
{
File file = new File("F:/myprog/start.bat");
Desktop.getDesktop().open(file);
} catch (IOException e)
{
e.printStackTrace();
}
jButton1ActionPerformed(evt);
}
When I run click the button I get "Error: Could not find or load main class"
Batch file :
javac *.java
java websphinx.workbench.Workbench
pause
When I click the bat file the application is running. But from Java program when I call this bat file I get the error. What went wrong?
In Java one usually runs a command while Runtime.getRuntime().exec, you'll need to pass cmd.exe as the file to run, and then the batch name as a parameter.
try {
Process p = Runtime.getRuntime().exec(
new String[]{"cmd.exe", "/c", "F:/myprog/start.bat"});
InputStream in = p.getInputStream();
OutputStream out = p.outputStream();
} catch (IOException e){
e.printStackTrace();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With