Jugi
Jugi

Reputation: 1254

Copy files from local window machine to remote windows machine using java

I have searched a lot but couldn't get a solution for this. I need to copy a file from local windows machine to remote windows machine using java program. I have tried with JSch,

JSch jsch = new JSch();
    Session session = null;
    session = jsch.getSession("username","hostname",22);
    session.setPassword("password");
    session.setConfig("StrictHostKeyChecking", "no");
        session.connect();
    ChannelSftp channel = null;
    channel = (ChannelSftp)session.openChannel("sftp");
    channel.connect();
        File localFile = new File("filePath");
        //If you want you can change the directory using the following line.
        channel.cd("E:/xxx");
    channel.put(new FileInputStream(localFile),localFile.getName());
        channel.disconnect();
    session.disconnect();

While executing the above code, i am facing the below error,

Exception in thread "main" 2: No such file
at com.jcraft.jsch.ChannelSftp.throwStatusError(ChannelSftp.java:2846)
at com.jcraft.jsch.ChannelSftp._realpath(ChannelSftp.java:2340)
at com.jcraft.jsch.ChannelSftp.cd(ChannelSftp.java:342)

I have cygwin installed in remote windows machine. It seems Jsch is not able to find the windows path. The same code works properly when copying files from windows machine to linux machine.

Please tel me a solution for the above problem or is there any other options for this to achieve in java ? Thanks

Upvotes: 0

Views: 2103

Answers (1)

nanofarad
nanofarad

Reputation: 41281

In order to resolve a Windows path with a drive letter you may need to use the /cygdrive prefix. In this case, your cd method call should be called with the parameter /cygdrive/e/xxx.

Upvotes: 3

Related Questions