Javaアプリケーション内でこのコマンドラインを実行する方法はありますか?Javaでコマンドラインを実行する
java -jar map.jar time.rel test.txt debug
私はコマンドで実行できますが、Javaでは実行できませんでした。
Javaアプリケーション内でこのコマンドラインを実行する方法はありますか?Javaでコマンドラインを実行する
java -jar map.jar time.rel test.txt debug
私はコマンドで実行できますが、Javaでは実行できませんでした。
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
これはコンソールに出力しようとすると、次のような出力しかないのが普通です:[email protected] – Ataman
'pr.getInputStream()'を使います。ここに詳細な例があります:http://www.linglom.com/2007/06/06/how-to-run-command-line-or-execute-external-application-from-java/ – kol
プロセスはと一緒に戻ります。あなたはpr.waitFor()でそれを得ることができます。したがって、次のようになります。 'int retVal = pr.waitFor()'。したがって、0でない場合は、中止またはクリーンアップできます。 – Shiki
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");
あなたはランタイムクラス内のexecコマンドを試してみましたか?あなたはそれ以上の問題に実行する場合Runtime - Java Documentation
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")
は、次のことを考えてみたが、私は上記のあなたのために働くことを推測している:
することができます次のような出力を見る:
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"を置く必要があります。
標準出力および/またはエラーで多くのデータを出力する場合に、呼び出されたプロセスがブロックされないようにするには、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();
}
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();
}
}
約私が試した、ランタイムRT = Runtime.getRuntime();プロセスPROC = rt.exec( "ピングローカルホスト")。 – Ataman
別のVM内からVMを起動しますか? – chance