2011-12-13 8 views
90

Javaアプリケーション内でこのコマンドラインを実行する方法はありますか?Javaでコマンドラインを実行する

java -jar map.jar time.rel test.txt debug 

私はコマンドで実行できますが、Javaでは実行できませんでした。

+0

約私が試した、ランタイムRT = Runtime.getRuntime();プロセスPROC = rt.exec( "ピングローカルホスト")。 – Ataman

+0

別のVM内からVMを起動しますか? – chance

答えて

141
Runtime rt = Runtime.getRuntime(); 
Process pr = rt.exec("java -jar map.jar time.rel test.txt debug"); 

http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html

+0

これはコンソールに出力しようとすると、次のような出力しかないのが普通です:[email protected] – Ataman

+7

'pr.getInputStream()'を使います。ここに詳細な例があります:http://www.linglom.com/2007/06/06/how-to-run-command-line-or-execute-external-application-from-java/ – kol

+5

プロセスはと一緒に戻ります。あなたはpr.waitFor()でそれを得ることができます。したがって、次のようになります。 'int retVal = pr.waitFor()'。したがって、0でない場合は、中止またはクリーンアップできます。 – Shiki

3
Process p = Runtime.getRuntime().exec("java -jar map.jar time.rel test.txt debug"); 
5
Runtime.getRuntime().exec("java -jar map.jar time.rel test.txt debug"); 
3

あなたはランタイムクラス内のexecコマンドを試してみましたか?あなたはそれ以上の問題に実行する場合Runtime - Java Documentation

8
import java.io.*; 

Process p = Runtime.getRuntime().exec("java -jar map.jar time.rel test.txt debug"); 

Runtime.getRuntime().exec("java -jar map.jar time.rel test.txt debug") 

は、次のことを考えてみたが、私は上記のあなたのために働くことを推測している:

Problems with Runtime.exec()

41

することができます次のような出力を見る:

final Process p = Runtime.getRuntime().exec("java -jar map.jar time.rel test.txt debug"); 

new Thread(new Runnable() { 
    public void run() { 
    BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream())); 
    String line = null; 

    try { 
     while ((line = input.readLine()) != null) 
      System.out.println(line); 
    } catch (IOException e) { 
      e.printStackTrace(); 
    } 
    } 
}).start(); 

p.waitFor(); 

Windowsで実行している場合は、コマンドの前に "cmd/c"を置く必要があります。

+4

p.getErrorStreamを使用して、コマンドが壊れている理由を理解することもできます。 – jakebeal

+0

@Craigo: "cmd/c"ステートメントに関する参考資料はありますか? thx – Campa

+2

実際には、 "copy"のようなwindowsコマンドを実行したい場合は、 "cmd/c"だけが必要だと思います。混乱の謝罪 – Craigo

14

標準出力および/またはエラーで多くのデータを出力する場合に、呼び出されたプロセスがブロックされないようにするには、Craigoが提供するソリューションを使用する必要があります。また、ProcessBuilderがRuntime.getRuntime()。exec()より優れていることにも注意してください。これはいくつかの理由からです:それは引数をより良くトークン化し、またエラー標準出力を処理します(hereも確認してください)。

ProcessBuilder builder = new ProcessBuilder("cmd", "arg1", ...); 
builder.redirectErrorStream(true); 
final Process process = builder.start(); 

// Watch the process 
watch(process); 

私は、このデータを新しいスレッドで収集するために新しい関数 "watch"を使用します。呼び出したプロセスが終了すると、このスレッドは呼び出しプロセスで終了します。

private static void watch(final Process process) { 
    new Thread() { 
     public void run() { 
      BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream())); 
      String line = null; 
      try { 
       while ((line = input.readLine()) != null) { 
        System.out.println(line); 
       } 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    }.start(); 
} 
3

public class CmdExec { 

public static Scanner s = null; 


public static void main(String[] args) throws InterruptedException, IOException { 
    s = new Scanner(System.in); 
    System.out.print("$ "); 
    String cmd = s.nextLine(); 
    final Process p = Runtime.getRuntime().exec(cmd); 

    new Thread(new Runnable() { 
     public void run() { 
      BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream())); 
      String line = null; 

      try { 
       while ((line = input.readLine()) != null) { 
        System.out.println(line); 
       } 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    }).start(); 

    p.waitFor(); 
    } 

} 
関連する問題