我正在尝试使用JSch在java中实现图形化ssh浏览器。我想检索ChannelSftp.ls
调用的结果,但它给了我一个nullpointer异常...我检查了会话,它是打开并登录。当我打开exec的通道并发送一个ls命令,我可以将目录内容检索为String。代码如下:
public Vector listContents () {
Channel channel = null;
ConnectionThread conn = SSHBrowser.conn;
Session sess = conn.getSession();
System.out.println(sess.getUserName()); //prints out the given username, not null, so the session is initialized
try {
channel = sess.openChannel("sftp");
} catch (JSchException ex) {
ex.printStackTrace();
}
Vector<ChannelSftp.LsEntry> result = null;
try {
result = ((ChannelSftp) channel).ls("/srv"); // throws nullpointer here
for (ChannelSftp.LsEntry dir : result) {
System.out.print(dir.getLongname());
}
} catch (SftpException e) {
e.printStackTrace();
}
return result;
}
我得到的堆栈跟踪如下:
at com.jcraft.jsch.ChannelSftp.ls(ChannelSftp.java:1747)
at com.jcraft.jsch.ChannelSftp.ls(ChannelSftp.java:1553)
at ssh.browser.ResultElement.listContents(ResultElement.java:69)
at ssh.browser.ConsoleCommand.lsTest(ConsoleCommand.java:68)
at ssh.browser.SSHBrowser$3.handle(SSHBrowser.java:113)
at ssh.browser.SSHBrowser$3.handle(SSHBrowser.java:103)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$KeyHandler.process(Scene.java:3964)
at javafx.scene.Scene$KeyHandler.access$1800(Scene.java:3910)
at javafx.scene.Scene.impl_processKeyEvent(Scene.java:2040)
at javafx.scene.Scene$ScenePeerListener.keyEvent(Scene.java:2501)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run(GlassViewEventHandler.java:217)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run(GlassViewEventHandler.java:149)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleKeyEvent$353(GlassViewEventHandler.java:248)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleKeyEvent(GlassViewEventHandler.java:247)
at com.sun.glass.ui.View.handleKeyEvent(View.java:546)
at com.sun.glass.ui.View.notifyKey(View.java:966)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
at com.jcraft.jsch.ChannelSftp.ls(ChannelSftp.java:1570)
... 37 more
我不知道这里有什么可能无效......你能帮忙吗?
答案 0 :(得分:5)
您还需要连接到频道。
ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
channel.connect();
for (Object entry : channel.ls("/srv")) {
System.out.println(entry);
}
查看SFTP example。