Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSch ChannelSftp.ls throws nullpointer

Tags:

java

jsch

I'm trying to implement a graphical ssh browser in java, with JSch. I would like to retrieve the result of a ChannelSftp.ls call, but it gives me a nullpointer exception... I checked the session, it is open and logged in. When I open the channel for exec and send an ls command, I can retrieve the directory contents as String. The code is the following:

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;
}

The stack trace, I get is the following:

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

I don't know what could possibly null here... Can you please help?

like image 364
Krisztián Dudás Avatar asked Dec 03 '22 13:12

Krisztián Dudás


1 Answers

You also need to connect to the channel.

ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
channel.connect();

for (Object entry : channel.ls("/srv")) {
    System.out.println(entry);
}

Have a look at the SFTP example.

like image 144
SubOptimal Avatar answered Dec 05 '22 03:12

SubOptimal