Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to execute bat file from a servlet?

Posted on 2001-06-03
6
Medium Priority
?
444 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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 one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

610 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