?
Solved

How to execute bat file from a servlet?

Posted on 2001-06-03
6
Medium Priority
?
441 Views
Last Modified: 2013-11-24
hello,

 How do you execute a bat file from a servlet?

-Dsys
0
Comment
Question by:Dsys
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 

Expert Comment

by:kamalindia
ID: 6151264
Use exec method of java.lang.Runtime class

Runtime r;

r=Runtime.getRuntime();

Process p;

p.exec("mybat.bat");

Regards

Raj
0
 

Author Comment

by:Dsys
ID: 6152116
import java.io.*;
import java.util.*;
import java.lang.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ExeStop extends HttpServlet {
     Runtime r;
     r=Runtime.getRuntime();
     Process p;
     p.exec("stopserver.bat");
     //does this have to reside in the same folder?
}

errors:
ExeStop.java:12: <identifier> expected
        r=Runtime.getRuntime();
         ^
ExeStop.java:14: <identifier> expected
        p.exec("stopserver.bat");
              ^
ExeStop.java:12: cannot resolve symbol
symbol  : class r
location: class ExeStop
        r=Runtime.getRuntime();
        ^
ExeStop.java:14: cannot resolve symbol
symbol  : class exec
location: package p
        p.exec("stopserver.bat");
         ^
4 errors

0
 
LVL 3

Accepted Solution

by:
chrisos earned 80 total points
ID: 6152583
Dsys,

The following code will run a file from within a servlet, I think the problem you ran into was a result of not putting your code inside a static initialiser or a method :)

I'm sure you already know how to reject an answer, so my advice is to do it, to get more input on this question!

Kamalindia,

Do not propose an answer unless you know it is an answer for certain, for guidance use the following from the experts handbook:

Please propose an "Answer" only when you are sure it will solve the questioner's problem. It is difficult for experts to find questions in need of answers when members interested in only obtaining points have locked questions.

Regards,

Chrisos

/////////////////////////////////////////////////////

package com.wintermuteis;

import java.io.*;
import java.util.*;
import java.lang.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ExeStop extends HttpServlet {

     private static Exception error = null;

     public static boolean runBatch() {
          boolean result = false;
          try {
               Runtime r = Runtime.getRuntime();
               String base = System.getProperty("user.dir");
               //As you can see I am prefixing the user's working directory to the target file name
               String target = base+File.separator+"stopserver.bat";
               System.out.println("Target: "+target);
               Process p = r.exec(target);
               result = true;
          }
          catch( IOException ioe ) {
               ioe.printStackTrace();
               error = ioe;
          }
          return result;
     }

     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
          response.setContentType("text/html");
          OutputStream outStream = response.getOutputStream();
          OutputStreamWriter out = new OutputStreamWriter(outStream);

          out.write("<HTML>\n");
          out.write("<HEAD>\n");
          out.write("<TITLE>Test</TITLE>\n");
          out.write("</HEAD>\n");
          out.write("<BODY>\n");
          out.write("<H1>Result:</H1> <BR />\n");
          out.flush();


          String action = request.getParameter("action");
          if( action == null || action.length() == 0 ) {
               action = "none";
          }
          if( action.equals("stopserver") ) {
               if( runBatch() ) {
                    out.write("Server has been shut down\n");
               }
               else {
                    error.printStackTrace(new PrintStream(outStream));
               }
          }
          else if( action.equals("none") ) {
               out.write("No action\n");
          }

          out.write("<BR />\n");
          out.write("</BODY>\n");
          out.write("</HTML>\n");

          out.flush();
          out.close();
          outStream.close();
     }

}
0
Stack Overflow Podcast - Developer Story

Welcome to the Stack Overflow podcast recorded Thursday July 20 at Stack Overflow Headquearters in NYC. Your hosts today are podcast regulars Jay Hanlon, David Fullerton, and Ilana Yitzhaki, plus the quite irregular Matt Sherman (Stack Overflow Engineering Manager extraordinaire)

 

Author Comment

by:Dsys
ID: 6154692
I am not very good with java please explain this part for me...

public static boolean runBatch() {
     boolean result = false;

     try {
              Runtime r = Runtime.getRuntime();
              //String base = System.getProperty("user.dir");
          String base = System.getProperty("C:/jsdk2.1");

              //As you can see I am prefixing the user's working directory to the target file name
              String target = base+File.separator+"stopserver.bat";
              System.out.println("Target: "+target);
              Process p = r.exec(target);
              result = true;
         }
         catch( IOException ioe ) {
              ioe.printStackTrace();
              error = ioe;
         }
         return result;
    }

When I run this servlet it does not execute the bat file to stop the app server? Don't I have to specify the file location?
0
 
LVL 3

Expert Comment

by:chrisos
ID: 6155121
Dsys,

The system property "user.dir" gets the working directory for the java virtual machine you are running the code from.

If you want to hard code the the directory just do the following:

String base = "C:\\jsdk2.1"

NOTE: Notice the double back slash, this is because this character is used to indocate an escape code, by doubling it you indicate that it is a literal.

File.separator is a constant that indicates the separateor used in paths to files on the current system:

/ for unix et al.
\ dor dos/windows et al.

Regards,

Chrisos.
0
 

Author Comment

by:Dsys
ID: 6155604
Thank you. I have to come up with a way to restart the app server once I take it down. Servlet wont be able to bring it back up if it cannot listen for it.
0

Featured Post

What is a Denial of Service (DoS)?

A DoS is a malicious attempt to prevent the normal operation of a computer system. You may frequently see the terms 'DDoS' (Distributed Denial of Service) and 'DoS' used interchangeably, but there are some subtle differences.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month9 days, 17 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question