I am using a self-coded tomcat servlet on my Raspberrypi. The Servlet works fine at least to the point where it should execute my python script on the Raspberry.
My Java Code looks like that:
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class GoogleServlet extends HttpServlet {
public static int i = 0;
public static String string = "";
public GoogleServlet() {
}
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletOutputStream output = resp.getOutputStream();
output.println("Hello There I am Raspberry DOS");
++i;
output.println("The number is: " + i);
String outputValue = req.getParameter("value");
if(outputValue == null) return;
output.println("Output Value: " + outputValue);
if (outputValue.equalsIgnoreCase("close light")) {
String pythonScriptPath = "/var/lib/tomcat8/webapps/ROOT/WEB-INF/classes/light.py";
String[] cmd = new String[2];
cmd[0] = "python";
cmd[1] = pythonScriptPath;
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(cmd);
}
}
}
This code works fine until it should run the python file called light.py which contains this content:
print('Hello Wordl')
When I call the website via. http://adress:8080/test?value=close%20light the Servlet sends to my PC "Hello There I am Raspberry DOS" and "The number is: 1" but on my Raspberry nothing happens.
I am sure that the python scrip which has by the way the permission 777 don't get executed in the right way, is it possible that the java code for executing a python file is wrong?