-2

I am trying to write code in java to check if a directory exists or not on remote server. I have tried something mentioned on Check whether the path exists on server or not in Java.

ChannelSftp channelSftp = new ChannelSftp();
SftpATTRS attrs = null;
try {
    String currentDirectory = channelSftp.pwd();
    System.out.println(currentDirectory);
} catch (SftpException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}
try {
    attrs = channelSftp.stat("/x/web/STAGE2MA49/qatools/capturescripts_new");
} catch (Exception e) {
    // TODO Auto-generated catch block
    System.out.println(" not found");
}

But the above code did work out. Below is the error stack

at com.jcraft.jsch.ChannelSftp.getHome(ChannelSftp.java:2443)
at com.jcraft.jsch.ChannelSftp.getCwd(ChannelSftp.java:2452)
at com.jcraft.jsch.ChannelSftp.pwd(ChannelSftp.java:2429)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:215)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:142)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:336)
at org.testng.SuiteRunner.run(SuiteRunner.java:268)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
at org.testng.TestNG.run(TestNG.java:1064)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:113)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:206)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:177)
Caused by: java.lang.NullPointerException
at com.jcraft.jsch.ChannelSftp.getHome(ChannelSftp.java:2435)
... 21 more
Mohammed Alaa
  • 3,140
  • 2
  • 19
  • 21
Raghu
  • 33
  • 6
  • "But that did work out. getting null pointer exception" should probably be "...did **not** work out...". Anyway NullPointerException is [very common problem](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) but without [mcve] we can't help you with debugging. – Pshemo Nov 26 '19 at 22:18
  • My guess is that, since path was not found `SftpATTRS attrs = null;` stayed as `null` so later actions which involve it like `attrs.someMethod();` would end up as as call to `null.someMethod();` but `null` doesn't have any methods and throws NPE. Either execute such code which rely on `attrs` value inside `try` section which assigns non-null value to it, or each time you use it check if it is not null like `if(attrs != null)` OR don't wrap it in `try` and rethrow any exception you face to client who executed your code, then he will have to decide how to handle such exceptions. – Pshemo Nov 26 '19 at 22:49

1 Answers1

1
ChannelSftp channelSftp = new ChannelSftp();

You can't just construct a ChannelSftp object and expect it to work. You will need to create an SSH session to a remote server, then request an SFTP channel to run through the session.

The question that you linked to references an example program which uses Jsch to make an SFTP connection to a remote server. I'll reproduce part of that here:

JSch jsch=new JSch();
...
Session session=jsch.getSession(user, host, port);
...
session.connect();
...
Channel channel=session.openChannel("sftp");
channel.connect();
ChannelSftp c=(ChannelSftp)channel;

Using the JSCH API, you'd create a Jsch object, then use that to create a Session, then use the Session object to connect to the remote server. Assuming that worked, you can request an "sftp" channel operating through the session. That returns the ChannelSftp object that you need.

Kenster
  • 23,465
  • 21
  • 80
  • 106