2

I'm running on a Linux machine a Java program that uses jcraft.jsch library to connect to an external sftp server. The code looks like:

JSch jsch = new JSch();
Session session = null;
Channel channel = null;  
ChannelSftp c = null;

session = jsch.getSession(ftpUserName, ftpHost, ftpPort);
session.setPassword(ftpPassword);

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

fn = c.ls("/Inbox");
c.cd("/Inbox");  //-- this line throws an error

For some reason when I run the change directory command "c.cd" I get:

4: Folder not found: /drwxr-x--- 2 ftpadmin ftpadmin        0 Jan 01 1970 /Inbox

It is weird because the listing (c.ls) of that folder does not throws an exception.

Furthermore, if I lftp from the command line from the same Linux server I can cd without any problems.

enter image description here

The stacktrace points to a _stat method inside the cd method.

    at com.jcraft.jsch.ChannelSftp.throwStatusError(ChannelSftp.java:2108)
    at com.jcraft.jsch.ChannelSftp._stat(ChannelSftp.java:1676)
    at com.jcraft.jsch.ChannelSftp.cd(ChannelSftp.java:290)
    at BW_Utilities.ftp.test.testFtpJsch(test.java:81)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

The folder structure of the remote site looks like the following when I connect using Filezilla from my desktop:

enter image description here

I just executed the same Java code on my windows desktop machine and the cd command worked. (Windows machine runs JDK 1.6.0_29 while the Linux server runs JRE 1.6.0.27)

Does jsch relies on some other library at the OS level at the client side?

Any idea how to proceed to troubleshoot this problem?

important UPDATE

I was able to reproduce the error on my dev machine. It got to do with jsch versions being used. The linux server is using jsch-0.1.31 while the dev machine uses jsch-0.1.52. It seems that whatever is causing the error is already solved in version 0.1.52. Wooot! Wooot! Finally!

Thanks

user1261620
  • 377
  • 2
  • 5
  • 12
  • You write `c.cd("/Inbox");`, but when you sftp in you do `cd Inbox/`. Have you tried excluding the leading slash in your `cd` call in java? `c.cd("Inbox");` – ug_ Jul 31 '16 at 00:07
  • Just tried with "Inbox", "/Inbox", "Inbox/", "/Inbox/", "./Inbox".. all of them giving the same problem. Also tried different folders in the SFTP with the same results. – user1261620 Jul 31 '16 at 01:41
  • have you tried using the absolute path to the `/Inbox` folder instead of the relative one? – ug_ Jul 31 '16 at 01:51
  • I think that "/Inbox" is an absolute path. – user1261620 Jul 31 '16 at 15:11
  • What is the SFTP server? Can you try to debug a call to `_realpath` from the `cd`? What is the input `path` and what does the server respond (the `i` and the `str` in the `_realpath`)? – Martin Prikryl Jul 31 '16 at 17:36
  • From my windows machine (where the cd commands works), if I run the following line of code: String realpath = c.realpath("/Inbox"); The value of realpath end up being "/Inbox" – user1261620 Jul 31 '16 at 21:23
  • Also, if I run int serverVersion = c.getServerVersion(); The value of serverVersion end up being 3 – user1261620 Jul 31 '16 at 21:31
  • But I asked about `_realpath` call from the `cd` + What does `session.getServerVersion()` return? – Martin Prikryl Aug 01 '16 at 05:43
  • I see you have solved it. Post your solution as an answer. Or delete your question. – Martin Prikryl Aug 01 '16 at 06:43

2 Answers2

2

Upgrading to jsch version 0.1.52 fixes the issue.

user1261620
  • 377
  • 2
  • 5
  • 12
0

Try doing the following.

fn = c.ls("Inbox");
c.cd("Inbox");  
Pradeep Pati
  • 5,779
  • 3
  • 29
  • 43