리눅스에서 GNOME 터미널 프로세스를 시작하고 입력 및 출력을 내 코드로 리디렉션하려면 java를 사용하십시오. 아래는 코드입니다.자바 프로세스 IO
public static void main(String[] args) throws InterruptedException
{
// TODO Auto-generated method stub /usr/bin/telnet
try
{
String line, commandInput;
Scanner scan = new Scanner(new File("/home/Ashutosh/Documents/testfile"));
ProcessBuilder telnetProcessBuilder = new ProcessBuilder("/bin/bash");///home/Ashutosh/Documents/tempScript");
telnetProcessBuilder.redirectErrorStream(true);
Process telnetProcess = telnetProcessBuilder.start();
//Process telnetProcess = Runtime.getRuntime().exec("xterm");///home/Ashutosh/Documents/tempScript");
BufferedReader input = new BufferedReader(new InputStreamReader(telnetProcess.getInputStream()));
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(telnetProcess.getOutputStream()));
while(scan.hasNext())
{
commandInput = scan.nextLine();
output.write(commandInput);
/* if(commandInput.trim().equals("exit"))
{
output.write("exit\r");
}
else
{
output.write("((" + commandInput + ") && echo --EOF--) || echo --EOF--\r");
}
*/ output.flush();
line = input.readLine();
while(line != null && !line.trim().equals("--EOF--"))
{
System.out.println(line);
line = input.readLine();
}
if(line == null)
break;
}
/* Thread.sleep(500);
output.write("/home/Ashutosh/Documents/testfile\r");
Thread.sleep(500);
output.flush();
while((line = input.readLine())!= null)
System.out.println(line);
telnetProcess.destroy();
*/ //String s = input.readLine();
//System.out.print(s + "\r\n");
//s = input.readLine();
//System.out.print(s + "\r\n");
}
bash는 스크립트 testfile 위의 내용은
#!/bin/bash
ls -l
pwd
date
exit
이며, 나 또한
#! /bin/bash
echo "Input any number form 0-3:"
read num
case $num in
0) echo "You are useless";;
1) echo "You are number 1";;
2) echo "Too suspecious";;
3) echo "Thats all man, got to go...!";;
*) echo "Cant't u read english, this is wrong choice";;
esac
read
exit
을 주어 내가 자바 코드에서 리디렉션하려는 사용자로부터 입력을 받아 다음과 같은 대화 형 스크립트를 시도
내 코드가 input.readLine();
에서 멈췄지만 출력 스트림을 리디렉션 할 수 없다고 생각합니다.
output.write(commandInput);
명령이 제대로 실행되고 있지만 리디렉션 된 입력에 대한 처리를 의도하지 않았습니다. 이유는 readLine();
입니다. 누군가 이미 해결했다면 해결책을 알려주십시오. 마지막에 줄 바꿈하지 않고,
Java Process with Input/Output Stream
감사
아슈
안녕하세요 베드로 후에도 코드로 프로세스 종료 후 지금 읽을 수있는 RAM의 문자열 버퍼를 유지 문자열에 명령을 읽은 후 "\ n"또는 "\ r"을 추가해도 아무런 변화가 없으며 무엇이 문제가 될 수 있는지 확실하지 않습니까? – Ashutosh