sh
'u etkileşimli modda bir alt işlem olarak yürüten bir Java programım var. Giriş System.in
'dan kopyalanır ve çıktı System.out
'a kopyalanır. Örneğin, çıktı bir yanlış sırada görünen bu interaktif kabuk komutları gibi pwd
çalıştırırken Her şey dışında çalışıyor:Çıktı neden alt işlemden yanlış sırada mı?
$ pwd
$ /home/viz/workspace
yerine
$ pwd
/home/viz/workspace
$
fark ait olduğunu birinci durumda $
istemi, pwd
çıkışından önce yazdırılır.
Neden olduğu hakkında herhangi bir fikir ve nasıl düzeltileceği? İşte
kodudur: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();
}
}
farklı bir kabuk Ne çalıştırmak olur? – dacwe
@dacwe: Sadece şu ana kadar çalışıyorum. Bash denedim ama IO yapmaya çalışırken durdu. Muhtemelen diğer bazı seçeneklere ihtiyaç duyar ('+ m' yardımcı olmaz). – vitaut