私はユーザーの入力を要求し、その入力をサーバー側のマシン上の外部プログラムのコマンドライン引数として渡す小さなWebアプリケーションを開発しています。Javaサーブレットから外部コマンドをどのように実行しますか?
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"のような単純なものをShellCommmandを実行するために渡しても、何もしません。私は正常にコマンドラインを介してコマンドを手動で実行することができます。
どうすればよいですか?
なぜ「分割」ですか? – davogotland
また、なぜあなたはwaitForから返された値を使用していませんか?それはあなたに何かを伝えるかもしれません。 – davogotland
これを実行しているURLを投稿できますか? (本当に、あなたはあなたのユーザ入力をエスケープしていません!!) –