File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes execute a batch file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic
Author

execute a batch file

Blikkies Marais
Ranch Hand

Joined: Jun 12, 2003
Posts: 50
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)
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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").


[Jess in Action][AskingGoodQuestions]
Blikkies Marais
Ranch Hand

Joined: Jun 12, 2003
Posts: 50
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

For the record, note that this does work -- he's just not seeing the output he expects. See this follow-up thread.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
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.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: execute a batch file
 
Similar Threads
log4j:ERROR
Struts!! will it work on websphere??
log4j:ERROR
deployment on websphere 4.0.1
Execute a batch file from java