나는 어떤 일이 벌어 질지 상상조차 할 수없는 문제가있다.Return BufferedReader
두 번째 클래스를 호출하여 명령을 통과하고 결과를 반환하는 클래스가 있습니다.
결과는 BufferedReader이며,이 결과는 첫 번째 클래스에서 처리됩니다.
일등석 :
.
.
.
String command = "ping " + ip + " -c 1";
BufferedReader result = instance.sendCommand(command);
// close only after all commands are sent
System.out.println("out of sshManager result : "+result);
System.out.println("out of sshManager line : "+result.readLine());
String line = null;
while ((line = result.readLine()) != null) {
System.out.println("out of sshManager line: "+line);
}
2 등석 :
public BufferedReader sendCommand(String command) throws JSchException, IOException {
//StringBuilder outputBuffer = new StringBuilder();
Channel channel = sesConnection.openChannel("exec");
((ChannelExec) channel).setCommand(command);
channel.connect();
InputStream commandOutput = channel.getInputStream();
System.out.println("This is in sshmanager SSHManager: " + commandOutput);
result = new BufferedReader(new InputStreamReader(commandOutput));
String line = null;
System.out.println(" sshmanager result : " + result());
System.out.println(" sshmanager result line : " + result.readLine());
while ((line = result.readLine()) != null) {
System.out.println("in sshmanager: " + line);
}
System.out.println("in sshmanager result : " + result);
channel.disconnect();
return result;
}
결과 :
This is in sshmanager SSHManager: [email protected]
sshmanager result : [email protected]
sshmanager result line : PING 192.168.11.11 (192.168.11.11) 56(84) bytes of data.
in sshmanager: From 192.168.11.77 icmp_seq=1 Destination Host Unreachable
in sshmanager:
in sshmanager: --- 192.168.11.11 ping statistics ---
in sshmanager: 1 packets transmitted, 0 received, +1 errors, 100% packet loss, time 3006ms
in sshmanager:
in sshmanager result: [email protected]
out of sshManager result: [email protected]
out of sshManager line: null
객체를 잘 내 두 번째 클래스에서 만든하지만 난 몰라 왜, 때 내 첫 번째 클래스에서 객체를 관리하려고하면 contect가 null입니다.
당신은 어떤 상상을하고 있습니까?
감사합니다. 작동하고 다른 작업을 위해 이러한 데이터를 보관해야하므로 좋은 생각입니다. Btw 거기에 내가 BufferedReader 이해가 안되는 이유는, 내가 두 번째 클래스의'readlince'에 주석을 달았고 while을 제거하면 그 결과는 여전히 null이기 때문입니다. 그러나,'System.out.println ("sshmanager result line :"+ result.readLine());을 유지하면 첫 번째 줄없이 첫 번째 클래스에서 결과를 읽을 수 있습니다. 조금 이상합니다. 그렇지 않습니다. ? – Sallyerik