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.
2 Answers
- Embed the script in the web application, at the root of the war file for example.
Get the path to the file once the application is deployed with:
String scriptPath = getServletContext().getRealPath("/script.sh");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.
3 Comments
getInputStream() on the Process created by ProcessBuilder to track the output of your script.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.