Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Servlets
|
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Servlets section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 23rd, 2008, 05:50 AM
Authorized User
 
Join Date: Aug 2008
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default Invoking a batch file from servlet

This must be really simple and yet I could not get it right. My need is to invoke a bat file from java servlet.

I could get the following info regarding the same but didn't work for me. The bat file is never invoked. It does not even enter catch block although it does not work properly. Any help?
try
{

String[] cmd = new String[3];


                cmd[0] = "cmd.exe" ;
                cmd[1] = "/C" ;
                cmd[2] = "C:\\Program Files\\apache-tomcat-6.0.18\\webapps\\mapping\\templatefileadjustment.b at";

Runtime.getRuntime().exec(cmd);
}
catch (IOException e) {

            response.getOutputStream().println(e);

            }


 
Old October 23rd, 2008, 10:38 PM
Authorized User
 
Join Date: Sep 2008
Posts: 87
Thanks: 1
Thanked 0 Times in 0 Posts
Default

hi

i think its not able to locate the path,

can you try this way and check whether its running the batch file

java.io.File file = new java.io.File("C:\\Program Files\\apache-tomcat-6.0.18\\webapps\\mapping\\templatefileadjustment.b at");

      String path = file.getCanonicalPath();
            Runtime.getRuntime().exec(path);


---Raj

 
Old October 24th, 2008, 12:00 AM
Authorized User
 
Join Date: Aug 2008
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I have tried that. No luck yet!

 
Old October 24th, 2008, 02:12 AM
Authorized User
 
Join Date: Aug 2008
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I could understand the problem.

(1)With out using start, it is not working.
 (2)After using start, I have also realized that the bat file is invoked from a different place from the one it is actually placed. In my case, it is called from C:\\Program Files\\apache-tomcat-6.0.18\\bin. So if the bat file contains any commands which are relative to the path it is located, it will not work

(3)Some thing like this will work

Runtime.getRuntime().exec("cmd.exe /c start C:\\templatefileadjustment.bat");

Note that the spaces in path will give an error and so I had placed my bat file right under C:






Similar Threads
Thread Thread Starter Forum Replies Last Post
Need Help with DOS batch file .. Arul20 Forum and Wrox.com Feedback 5 August 31st, 2007 06:51 AM
Invoking Help File MichaelRReed1939 VB How-To 1 October 26th, 2006 10:37 AM
Batch File Allan320 Access 10 June 8th, 2006 06:46 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.