대화 형 모드에서 서브 프로세스로 sh
을 실행하는 Java 프로그램이 있습니다. 입력은 System.in
에서 복사되고 출력은 System.out
으로 복사됩니다. 예를 들어, 출력이 잘못된 순서로 나타납니다이 대화 형 쉘에서 명령과 같은 pwd
을 실행할 때 모든 것을 제외하고 잘 작동 :하위 프로세스의 출력이 잘못된 순서로 나타나는 이유는 무엇입니까?
$ pwd
$ /home/viz/workspace
대신
$ pwd
/home/viz/workspace
$
의 차이입니다 그 첫 번째 경우에 pwd
의 출력 전에 프롬프트 $
이 인쇄됩니다.
왜 그런 일이 일어나고 어떻게 해결할 수 있습니까? 여기
코드입니다 :import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
class StreamCopier implements Runnable {
private InputStream in;
private OutputStream out;
public StreamCopier(InputStream in, OutputStream out) {
this.in = in;
this.out = out;
}
public void run() {
try {
int n;
byte[] buffer = new byte[4096];
while ((n = in.read(buffer)) != -1) {
out.write(buffer, 0, n);
out.flush();
}
out.close();
}
catch (IOException e) {
System.out.println(e);
}
}
}
public class Test {
public static void main(String[] args)
throws IOException, InterruptedException {
Process process = Runtime.getRuntime().exec("sh -i +m");
Thread outThread = new Thread(new StreamCopier(
process.getInputStream(), System.out));
outThread.start();
Thread errThread = new Thread(new StreamCopier(
process.getErrorStream(), System.err));
errThread.start();
Thread inThread = new Thread(new StreamCopier(
System.in, process.getOutputStream()));
inThread.start();
process.waitFor();
outThread.join();
errThread.join();
inThread.join();
}
}
가 다른 쉘을 어떻게 실행하면 어떻게됩니까을? – dacwe
@dacwe : 지금까지'sh' 작업 만 할 수있었습니다. 시도 'bash는'하지만 IO를하려고하면 중지됩니다. 아마도 다른 옵션이 필요할 것입니다 ('+ m'은 도움이되지 않습니다). – vitaut