0

i want to run a bash script with java ee / servlets . I want it embedded in a web application , using tomcat 7 in a linux os.

1
  • 1
    c'mon boots, start walking. (that is, tell us what you have tried, and what sort of error messages you're getting). Good luck. Commented May 28, 2012 at 20:10

2 Answers 2

3
  1. Embed the script in the web application, at the root of the war file for example.
  2. Get the path to the file once the application is deployed with:

    String scriptPath = getServletContext().getRealPath("/script.sh");

  3. Run the script with the ProcessBuilder class:

    new ProcessBuilder("/bin/sh", scriptPath).start();

Note that the step 2 may fail if you servlet container doesn't extract the content of the war file. In this case you'll have to put the script as a resource available on the classpath, copy its content into a temporary file, and run the script there.

Sign up to request clarification or add additional context in comments.

3 Comments

There are security issues involving this solution, so it should be used carefully. If you put a script inside the war, the user can be able to see the script source code. IMO is better to use the CGI solution, since it is intended to handle external programs by design.
1-ok.2 and 3 : i have them in my servlet?How to know if 3. is working?
You can call getInputStream() on the Process created by ProcessBuilder to track the output of your script.
2

You must search how to enable and use CGI on Tomcat.

The CGI (Common Gateway Interface) defines a way for a web server to interact with external content-generating programs, which are often referred to as CGI programs or CGI scripts.

Source: Apache Tomcat 7 Documentation: CGI How To

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.