Question

I am using JSCH library to do a sftp between two servers. But, I am unable to load the file into the URL using getResource(), for which I get an error during runtime. I checked the permissions and they are 600, so we should be able to read it.

    JSch jsch = new JSch();
    File file = new File("/home/xxxxxx/.ssh/id_rsa");
    Session session = null;
    URL keyFileURL = null;
    URI keyFileURI = null;
    if (file.exists())
    {
        keyFileURL = SecureFTP.class.getClassLoader().getResource("/home/xxxxxx/.ssh/id_rsa");
        if (keyFileURL == null)
        {
            System.out.println("what");
            throw new RuntimeException("Key file not found in classpath");
        }
    }
  try{
             keyFileURI = keyFileURL.toURI();
    }
    catch(Exception URISyntaxException)
    {
        System.out.println("Wrong URL. LOL");
}



    jsch.addIdentity(new File(keyFileURI).getAbsolutePath());

Is there something I am missing, in loading the resource or the path?

Was it helpful?

Solution

Why don't you use the File.toURL

File file = new File("/home/dev85adm/.ssh/id_rsa");
if (file.exists()) {
    URL keyFileURL = file.toURL();
    //...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top