Reputation: 369
Hi i want to write a java program in Linux machine which should read a file in another remote Linux machine and copy its contents to the source machine. I am using the following code for it
JSch jsch = new JSch();
Session session = jsch.getSession(user, host, port);
session.setPassword(password);
session.setConfig("StrictHostKeyChecking", "no");
System.out.println("Establishing Connection...");
session.connect();
System.out.println("Connection established.");
System.out.println("Crating SFTP Channel.");
ChannelSftp sftpChannel = (ChannelSftp) session.openChannel("sftp");
sftpChannel.connect();
System.out.println("SFTP Channel created.");
InputStream out = null; //.get(remoteFile);
out = sftpChannel.get(pub);
System.out.println("Read Successful");
System.out.println(pub);
StartString = pub.split("/");
i=StartString.length;
fileName =LocalWrite+StartString[i-1];
System.out.println(fileName);
OutputStream fileOut = new FileOutputStream(new File(fileName));
byte[] buf = new byte[1024];
int len;
while ((len = out.read(buf)) > 0) {
fileOut.write(buf, 0, len);
}
System.out.println("Wrote Successfull");
out.close();
fileOut.close();
sftpChannel.disconnect();
session.disconnect();`
When i try this i am getting a fileNotFound Exception but when i try the same code in Windows Machine i am able to read the file and copy its contents to my local machine. Could you tell me where i am doing the mistake.
Upvotes: 0
Views: 1996
Reputation: 369
Hi the problem has been solved. The two machines are in Different Networks hence the above code was not working.
Upvotes: 0
Reputation: 83577
Hard to tell without more information. A wild guess (suggested in the comments): Did you use the correct pathname for the Linux system (which will be different to the name on Windows)? Did you pay attention to upper/lower case?
To debug this further, you could run sshd
(the SSD daemon) in debug mode on the target Linux system. Then try to connect, and see what file name arrives on the target system, and why it does not find it.
Upvotes: 1