将addProtocolCommandListener与库org.apache.commons.net.telnet.TelnetClient一起使用时出现未知来源异常

时间:2016-12-27 19:49:42

标签: java apache telnet apache-commons-net

我使用以下库...

org.apache.commons.net.telnet.TelnetClient;

我有这段代码用于连接服务器。

import org.apache.commons.net.*;
import org.apache.commons.net.ProtocolCommandListener
import org.apache.commons.net.ftp.FTPSClient
import org.apache.commons.net.telnet.TelnetClient;


String server = "some server";
String result = '';

//Create the telnet client and connect on port 43
TelnetClient telnetClient = new TelnetClient();
telnetClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
telnetClient.connect(server, 43);

当我到达....

telnetClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));

我得到以下异常....

抓住:java.lang.NullPointerException 断开与目标VM的连接,地址:' 127.0.0.1:3140',transport:' socket' 显示java.lang.NullPointerException     在org.apache.commons.net.SocketClient.addProtocolCommandListener(SocketClient.java:772)     在org.apache.commons.net.SocketClient $ addProtocolCommandListener.call(未知来源)

有人知道为什么我会得到这个例外吗? 如果我删除了该行,那么异常就是这一行。如果我使用FTPS客户端,我在同一个库中没有SFTP这个问题!

嗨,感谢您的回复,这是图书馆中一些支持的客户端的简单示例,我猜测whois和ftp客户端可能不支持列表器!?

   import org.apache.commons.net.*;
   import org.apache.commons.net.ProtocolCommandListener;
   import org.apache.commons.net.ftp.FTPClient;
   import org.apache.commons.net.telnet.TelnetClient;
   import org.apache.commons.net.whois.WhoisClient;
   import java.io.PrintWriter;

   public class WhoisExample
   {
       public static void main(String args[])
       {
           try {
               WhoisClient whoisClient = new WhoisClient();
               whoisClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
           }
           catch(Exception e) {
               System.out.println("whois client exception" + e);
           }

           try {
               TelnetClient telnetClient = new TelnetClient();
               telnetClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
           }
           catch(Exception e) {
               System.out.println("telnet client exception" + e);
           }

           try {
               FTPClient ftpClient = new FTPClient();
               ftpClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));

               System.out.println("no ftp client exception here !");
           }
           catch(Exception e)
           {
               System.out.println("ftp client exception" + e);
           }
       }
   }

1 个答案:

答案 0 :(得分:0)

我记录了Apache commons net的一个错误,它已被解决为没问题。

基本上......有些客户目前不支持ProtocolCommand Listener。'

您可以从以下链接中找到更多信息....

https://issues.apache.org/jira/browse/NET-608