bash unix processbuilder from java not running

梦想的初衷 提交于 2019-12-07 13:30:46

问题


I want to execute a simple Unix command from my Java servlet: what I need to do is a simple echo write to file like this one:

echo HELLO > myfile.txt

What I'm doing in my servlet is:

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServletAutorecovery extends HttpServlet {
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            ProcessBuilder pb = new ProcessBuilder("/usr/bin/bash", "-c", "echo HELLO > ../webapps/test/myfile.txt");
            pb.start();
        } finally { 
        out.close();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        processRequest(request, response);
    }
}

My problem is: this code section is not giving me any errors, but nothing happens. After I executed my servlet, the file has not being created, and of course, nothing is written in it.

What am I doing wrong?

EDIT1: added full path to pb command.

EDIT2: bash is in the path /usr/bin/bash, 100% sure of it.

EDIT3: added SSCCE.


回答1:


First, are you sure bash is definitely at /usr/bin? Second, you probably need to tell the ProcessBuilder what directory it should use as the cwd when running the process, otherwise it will try and create myfile.txt in whatever is the current directory of the servlet container, typically somewhere you don't have write access. And thirdly, when you run a process from java the output of the process is passed back to java via input streams on the process object, it doesn't go straight to stdout, so you need to read the streams to see the result

ProcessBuilder pb = new ProcessBuilder("/usr/bin/bash", "-c", "echo HELLO > myfile.txt");
pb.directory(...);
pb.redirectErrorStream(true);
Process p = pb.start();
IOUtils.copy(p.getInputStream(), System.out);
p.waitFor();



回答2:


String echo = "echo 'hello' > myfile.txt";
ProcessBuilder pb = new ProcessBuilder("/usr/bin/bash", "-c", echo);
pb.start();



回答3:


Check your error handling; you're probably swallowing an exception somewhere because there is no bash in /usr/bin, so you're getting a "file not found" exception (or similar).

Try "/bin/bash" instead. The rest should work.

Also note that relative paths won't work after you deploy you app since it will be relative to the process running the Java VM which isn't what you expect, want or could use. Ask your ServletContext for a path with getRealPath()



来源:https://stackoverflow.com/questions/14703271/bash-unix-processbuilder-from-java-not-running

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!