ProcessBuilder
で始まります。あなたはコマンドを送信したい各パラメータは、例えば...
import java.io.IOException;
import java.io.InputStream;
public class Test {
public static void main(String[] args) throws IOException, InterruptedException {
ProcessBuilder pb = new ProcessBuilder(
"whiptail", "--title", "Check list example", " --checklist",
"Choose user's permissions", "20", "78", "4",
"NET_OUTBOUND", "Allow connections to other hosts", "ON",
"NET_INBOUND", "Allow connections from other hosts", "OFF",
"LOCAL_MOUNT", "Allow mounting of local devices", "OFF",
"REMOTE_MOUNT", "Allow mounting of remote devices", "OFF");
pb.redirectInput(Redirect.INHERIT);
// I tend to use pb.redirectErrorStream(true);
// which sends the error stream to the input stream, but
// then you'd need to still consume it to get the result
Process p = pb.start();
InputStreamConsumer errorConsumer = new InputStreamConsumer(p.getErrorStream());
Scanner input = new Scanner(System.in);
String option = input.nextLine();
p.getOutputStream().write(option.getBytes());
p.getOutputStream().flush();
int exitCode = p.waitFor();
System.out.println(exitCode);
errorConsumer.join();
System.out.println(errorConsumer.getContent());
}
public static class InputStreamConsumer extends Thread {
private InputStream is;
private StringBuilder content;
public InputStreamConsumer(InputStream is) {
this.is = is;
content = new StringBuilder(128);
}
public String getContent() {
return content.toString();
}
@Override
public void run() {
try {
int value = -1;
while ((value = is.read()) != -1) {
content.append((char)value);
}
} catch (IOException exp) {
exp.printStackTrace();
}
}
}
}
これは、それは単にコマンドを非常に基本的なコマンドリスト内の個別の要素実行され、StringBuilder
(検索するにそれの出力を消費後で)、コマンドが存在するまで待って基本的な結果を表示します。
私はにアクセスできないので、コードをテストすることはできませんが、コマンドがOSのデフォルトの検索パスで利用可能な場合は動作するはずです。それ以外の場合はパスを指定する必要がありますコマンドリストの最初の要素の一部としてコマンドに追加
これはあなたの最初の投稿であることを理解しています。ようこそ!このフォーラムでは、どのようにして質問をあまりにも曖昧にする傾向があります。通常、何かを試してみて、コードをいくつか表示し、なぜそれがあなたが期待していることをしているのか、それともしていないのかを尋ねますその精神の中で、単にwhiptailを呼び出して、それを表示させることができるかどうかを調べてみてください。その後、結果を戻すことを混乱させることができます。 ところで、JOptionPaneはコード内で同様の機能を実行します。あなたが望むことをするためにSwingプログラミングを少ししなければならないでしょうが、それはもっと簡単になるかもしれません。 – Steve11235