시작은 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
(검색 할로 출력의 소비 나중에) 명령이 존재할 때까지 대기하고 기본 결과를 표시합니다.
whiptail
에 액세스 할 수 없으므로 코드를 테스트 할 수는 없지만 명령이 OS의 기본 검색 경로에서 사용 가능하면 작동해야합니다. 그렇지 않으면 경로를 입력해야합니다 명령 목록의 첫 번째 요소의 일부로 명령에
나는 이것이 첫 번째 게시물이라는 것을 알고 있습니다. 환영합니다! "이 질문에 대한 질문은 지나치게 모호합니다. 일반적으로 무언가를 시도하고 코드를 표시하며 예상되는대로 또는하지 않는지 묻습니다. 그 정신으로 단순히 whiptail을 호출하여 표시되도록하십시오. 그런 다음 결과를 다시 가져 오는 것을 망칠 수 있습니다. BTW, JOptionPane은 코드 내에서 유사한 기능을 수행합니다. 당신이 원하는 것을하기 위해 약간의 스윙 프로그래밍을해야 할 것이지만, 그것은 더 쉽게 끝날 수 있습니다. – Steve11235