0

I am working on an Sftp connection where the code is

import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
  
public class SftpExample {
    public static void main(String[] args) {
        String host = "ABCDAWSFTP01";
        String user = "jira2timesheet";
        String password = "pass";
        int port = 22;
          
        try {
            JSch jsch = new JSch();
            Session session = jsch.getSession(user, host, port);
            session.setPassword(password);
            session.setConfig("StrictHostKeyChecking", "no");
            session.connect();
            
            System.out.println("Connected with server");
              
            // Perform SFTP operations here
           
            session.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

By adding the jar jsch-0.1.55.jar and running the following commands, I get the following response

>C:\Users\dhs\Desktop\javacode>javac -cp jsch-0.1.55.jar SftpExample.java

>C:\Users\dhs\Desktop\javacode>java SftpExample
Exception in thread "main" java.lang.NoClassDefFoundError: com/jcraft/jsch/JSch
        at SftpExample.main(SftpExample.java:12)
Caused by: java.lang.ClassNotFoundException: com.jcraft.jsch.JSch
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 1 more

I'm unable to understand the exception. Can someone please guide me?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
dhS
  • 3,739
  • 5
  • 26
  • 55
  • *Connected with server* - works for me. If you add the classpath to `java SftpExample` – Scary Wombat Apr 18 '23 at 02:40
  • @ScaryWombat could you please write the command – dhS Apr 18 '23 at 02:43
  • 1
    No. With respect, part of being a programmer is to know how to search for such things. – Scary Wombat Apr 18 '23 at 02:44
  • set CLASSPATH=%Path%;C:\Users\dhs\Desktop\javacode\jsch-0.1.55.jar; this work for me – dhS Apr 18 '23 at 03:59
  • awesome. Didn't need me after all. – Scary Wombat Apr 18 '23 at 04:04
  • Including the PATH in CLASSPATH is not correct. In any case, the better solution would be to specify the classpath also when running `java` (i.e. `java -cp .;jsch-0.1.55.jar SftpExample`), because setting CLASSPATH can have issues when you're running multiple Java applications with incompatible classpaths. – Mark Rotteveel Apr 18 '23 at 12:33

0 Answers0