This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
How do I execute a batch filr from a java application? I have tried: String sLocation = "X:\\Java\\WSAD\\workspace\\xrad2\\webApplication\\xrad\\config\\startup.bat"; Runtime rt = Runtime.getRuntime(); try { rt.exec(sLocation); System.out.println("Process: " + sLocation ); } catch(IOException e) { Logger.logError("IOException starting process! Location: "+sLocation); e.printStackTrace(); } But I get the following error: ERROR: IOException starting process! Location: X:\Java\WSAD\workspace\xrad2\webApplication\xrad\config\startup.bat java.io.IOException: CreateProcess: X:\Java\WSAD\workspace\xrad2\webApplication\xrad\config\startup.bat error=193 at java.lang.Win32Process.create(Native Method) at java.lang.Win32Process.<init>(Win32Process.java:70) at java.lang.Runtime.execInternal(Native Method) at java.lang.Runtime.exec(Runtime.java:568) at java.lang.Runtime.exec(Runtime.java:433) at java.lang.Runtime.exec(Runtime.java:376) at java.lang.Runtime.exec(Runtime.java:340) at com.orygen.xrad.util.RuntimeExec.execute(RuntimeExec.java:45) at com.orygen.xrad.webenvironment.ExradProcessor.startupXrad(ExradProcessor.java:2265) at com.orygen.xrad.webenvironment.ExradProcessor.initialise(ExradProcessor.java:274) at com.orygen.xrad.webenvironment.ExradControllerServlet.init(ExradControllerServlet.java:41) at javax.servlet.GenericServlet.init(GenericServlet.java:258) at com.ibm.servlet.engine.webapp.StrictServletInstance.doInit(ServletManager.java:802) at com.ibm.servlet.engine.webapp.StrictLifecycleServlet._init(StrictLifecycleServlet.java:141) at com.ibm.servlet.engine.webapp.PreInitializedServletState.init(StrictLifecycleServlet.java:254) at com.ibm.servlet.engine.webapp.StrictLifecycleServlet.init(StrictLifecycleServlet.java:107) at com.ibm.servlet.engine.webapp.ServletInstance.init(ServletManager.java:388) at javax.servlet.GenericServlet.init(GenericServlet.java:258) at com.ibm.servlet.engine.webapp.ServletManager.addServlet(ServletManager.java:84) at com.ibm.servlet.engine.webapp.WebAppServletManager.loadServlet(WebAppServletManager.java:226) at com.ibm.servlet.engine.webapp.WebAppServletManager.loadAutoLoadServlets(WebAppServletManager.java:357) at com.ibm.servlet.engine.webapp.WebApp.loadServletManager(WebApp.java:1010) at com.ibm.servlet.engine.webapp.WebApp.init(WebApp.java:133) at com.ibm.servlet.engine.srt.WebGroup.loadWebApp(WebGroup.java:234) at com.ibm.servlet.engine.srt.WebGroup.reload(WebGroup.java:1033) at com.ibm.servlet.engine.srt.ClassChangeWatcher.classChanged(ClassChangeWatcher.java:55) at com.ibm.ws.classloader.ClassLoaderManager.checkAndNotify(ClassLoaderManager.java:380) at com.ibm.ws.classloader.ClassLoaderManager.alarm(ClassLoaderManager.java:291) at com.ibm.ejs.util.am.AlarmThread.run(AlarmThread.java:56)
Error 193 is ERROR_BAD_EXE_FORMAT, because a .bat file isn't an executable. You've got to start cmd.exe and tell it to run the batch file -- i.e., exec("cmd /c path\\to\\foo.bat").
Thanks for the response, but I still have a problem. I try the following: rt.exec("cmd X:\\Java\\WSAD\\workspace\\xrad2\\webApplication\\xrad\\config\\startup.bat"); It executes a dos prompt, but does not execute the batch file. But I don't get any Exception. I have also tried: rt.exec("cmd /c X:\\Java\\WSAD\\workspace\\xrad2\\webApplication\\xrad\\config\\startup.bat"); But this does not execute the batch file either. Have you any suggestions?
Ernest Friedman-Hill
author and iconoclast
Marshal
Uh oh. This conversation in two places could get a bit confusing. I'm closing this thread, and let's continue the conversation over in the Intermediate forum.