0
votes

I'm trying to read a file via sftp protocol using JSch library:

final JSch jsch = new JSch();
this.session = jsch.getSession("me","myserver.com",22);
session.setPassword("redacted");

//this is just a test, so I don't care about security
Properties props = new Properties();
props.put("StrictHostKeyChecking", "no");
session.setConfig(props);

session.connect();
final ChannelSftp channel = (ChannelSftp)session.openChannel("sftp");
InputStream is = channel.get("/home/me/totallyexistingfile.txt";

The problem is I'm getting the following exception on the last line of the code:

Exception in thread "main" java.io.IOException: Cannot read remote file
at com.teamead.ino.datauser.logproc.source.SSHLogSource.getStream(SSHLogSource.java:51)
at com.teamead.ino.datauser.logproc.processing.LogProc.main(LogProc.java:37)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: 4: 
at com.jcraft.jsch.ChannelSftp.get(ChannelSftp.java:1513)
at com.jcraft.jsch.ChannelSftp.get(ChannelSftp.java:1266)
at com.teamead.ino.datauser.logproc.source.SSHLogSource.getStream(SSHLogSource.java:48)
... 6 more
Caused by: java.lang.NullPointerException
at com.jcraft.jsch.ChannelSftp.get(ChannelSftp.java:1287)
... 8 more

I've done some googling and found out that this error probably comes from the sftp server and usually happens during upload(out of space, etc). So I've setup logging on the sftp server(DEBUG level) and only see this:

Oct 21 16:41:14 me sshd[365]: Accepted password for me from 192.211.54.146 port 59458 ssh2

Nothing else. I have no idea what else can I investigate, because I get all relevant logs from both sides :( The remote machine is a Debian with OpenSSH.

1

1 Answers

2
votes

Turned out I was only missing channel.connect()