我正在開發一個小型web應用程序,它要求用戶輸入並將該輸入作爲服務器端計算機上外部程序的命令行參數傳遞。如何從Java servlet運行外部命令?
public class WorkflowServlet extends HttpServlet
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String workflow = request.getParameter("workflow");
String preInflation = request.getParamater("preInflation");
String email = request.getParamater("email");
try {
executeShellCommand("java ClusterProcess " + username + " "
+ workflow + " " + preInflation + " " + email);
} catch (Exception e) {
response.sendRedirect("WorkflowAction.jsp"); return;
}
response.sendRedirect("WorkflowInProgress.jsp");
}
}
public static void executeShellCommand(String command) {
Runtime.getRuntime().exec(command.split(" ")).waitFor();
}
}
沒有發生任何異常 - 它似乎什麼都不做。即使我傳入一些非常簡單的東西,例如「touch test.txt」來執行executeShellCommand,它也不會執行任何操作。我可以通過命令行手動成功運行該命令。
我該做什麼?
爲什麼「分裂」? – davogotland 2012-01-05 01:04:22
另外,爲什麼你不使用waitFor的返回值?它可能會告訴你一些事情。 – davogotland 2012-01-05 01:08:26
你可以發佈它正在運行的URL嗎? (沒有,請勿 - 您不會逃避用戶輸入!!) – 2012-01-05 01:14:54