2012-01-05 16 views
3

私はユーザーの入力を要求し、その入力をサーバー側のマシン上の外部プログラムのコマンドライン引数として渡す小さな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を実行するために渡しても、何もしません。私は正常にコマンドラインを介してコマンドを手動で実行することができます。

どうすればよいですか?

+0

なぜ「分割」ですか? – davogotland

+0

また、なぜあなたはwaitForから返された値を使用していませんか?それはあなたに何かを伝えるかもしれません。 – davogotland

+0

これを実行しているURLを投稿できますか? (本当に、あなたはあなたのユーザ入力をエスケープしていません!!) –

答えて

2

入力ストリームまたはエラーストリームをキャプチャしないと、プロセスからの潜在的なフィードバックがありません。私は前に書いたものから次のコード(私のIDEの快適さの外)を修正しました。明白な誤りがある場合はお詫びします。

import java.io.BufferedReader; 
import java.io.File; 
import java.io.IOException; 
import java.io.InputStreamReader; 
... 

String[] commands = {"/usr/bin/touch", "/home/blah/test.txt"}; 
//this could be set to a specific directory, if desired 
File dir = null; 
BufferedReader is = null; 
BufferedReader es = null; 

try 
{ 
    Process process; 
    if (dir != null) 
     process = Runtime.getRuntime().exec(commands, null, directory); 
    else 
     process = Runtime.getRuntime().exec(commands); 
    String line; 
    is = new BufferedReader(new InputStreamReader(process.getInputStream())); 
    while((line = is.readLine()) != null) 
     System.out.println(line); 
    es = new BufferedReader(new InputStreamReader(process.getErrorStream())); 
    while((line = es.readLine()) != null) 
     System.err.println(line); 

    int exitCode = process.waitFor(); 
    if (exitCode == 0) 
     System.out.println("It worked"); 
    else 
     System.out.println("Something bad happend. Exit code: " + exitCode); 
} //try 
catch(Exception e) 
{ 
    System.out.println("Something when wrong: " + e.getMessage()); 
    e.printStackTrace(); 
} //catch 
finally 
{ 
    if (is != null) 
     try { is.close(); } catch (IOException e) {} 
    if (os != null) 
     try { es.close(); } catch (IOException e) {} 
} //finally 
+0

コードでは問題は解決しません。サーブレットは適切にリダイレクトされません。 – artaxerxe

+2

@artaxerxe - あなたが何を意味するか分かりません。元の投稿にリダイレクトについては何もありません。さらに、私が提供したコード例は、入力ストリームとエラーストリームを読み取ることを示すことでした。サーブレットの例ではありませんでした。 –

1

あなたは何かを混乱させていて、検索パスのような素晴らしいものを持っているシェルを使用しています。

exec()のプロセスの絶対パスを指定します。例えば/usr/bin/touchまたは/path/to/java

関連する問題