내가 달성하고 싶은 것은 내가
만든 자바 애플리케이션에서 소프트웨어를 설치하는 것 입니다. gksudo에게 허가를 받았습니다. 그런 다음 패스를 입력하고 예 / 아니요 옵션이 나타날 때까지 프로그램이 작동하기 시작했습니다. 이 질문을 어떻게 통과 할 수 있습니까?
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class TestApp {
public static void main(String[] args) {
Process ls = null;
BufferedReader input = null;
String line = null;
try {
ls = Runtime.getRuntime().exec(
new String[] { "gksudo", "apt-get", "install", "PACKAGE" });
try {
ls.wait(6000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ls = Runtime.getRuntime().exec(new String[] { "y" });
input = new BufferedReader(new InputStreamReader(
ls.getInputStream()));
} catch (IOException e1) {
e1.printStackTrace();
System.exit(1);
}
try {
while ((line = input.readLine()) != null) {
System.out.println(line);
}
ls.destroy();
} catch (IOException e1) {
e1.printStackTrace();
System.exit(0);
}
}
}
쉬움-다소 특별한-해결책 : "apt-get install ..."대신 "apt-get -y install ..."을 실행하지 마십시오. 확인을위한 질문이 표시되지 않습니다.
보다 일반적인 접근 방식 : Runtime.exec ()는 프로세스 인스턴스를 생성합니다. 코드에서 수행하는 작업은 "apt-get"과 "y"의 두 가지 프로세스를 만드는 것입니다. 해야 할 일은 하나의 프로세스 인스턴스를 만들고이 프로세스의 입력 채널에 문자 "y"를 입력하는 것입니다. 이것은 프로세스를 생성하고 (이미 수행 한 것처럼) 입력에 대한 참조를 수집하고이 입력에 "y \ n"을 전송하여 수행됩니다.
입력은 프로세스의 입력에 연결된 OutputStream을 제공하는 Process.getOutput ()을 호출하여 얻습니다.
이 모든 결과는 다음과 같습니다.
ls = Runtime.getRuntime().exec(
new String[] { "gksudo", "apt-get", "install", "PACKAGE" });
ls.getOutput().write("y\n".getBytes());
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다