2

I have problem with short script JSCH which I'm using ind Oracle Data Integrator. I want to send file from directory to server SFTP. My code:

import com.jcraft.jsch.*;

JSch ssh = new JSch();
Session session = ssh.getSession("pas", "host", 22);

session.setConfig("StrictHostKeyChecking", "no");
session.setConfig(
        "PreferredAuthentications", 
        "publickey,keyboard-interactive,password");
session.setPassword("pass");

session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();

ChannelSftp sftp = (ChannelSftp) channel;

sftp.put("C:\Tools\tmp.html", "/var/temp/temp.html");

channel.disconnect();
session.disconnect();

I get error:

ODI-1590: The execution of the script failed. 
Caused By: org.apache.bsf.BSFException: BeanShell script error: Sourced file:
inline evaluation of: ``
import oracle.odi.km.exception.OdiKMException;
import com.jcraft.jsch.*;
J . . . '' Token Parsing Error: Lexical error at line 23, column 19. 
Encountered: "W" (87), after : "\"C:\\": <at unknown location>
BSF info: upload file at line: 0 column: columnNo

What I'm doing wrong?

deHaar
  • 17,687
  • 10
  • 38
  • 51
user3764298
  • 47
  • 1
  • 9
  • please try sftp.put("C:/Tools/tmp.html", "/var/temp/temp.html"); – dassum Mar 03 '20 at 12:06
  • 1
    Try `sftp.put("C:\\Tools\\tmp.html", "/var/temp/temp.html");` – deHaar Mar 03 '20 at 12:18
  • @deHaar - your solution has helped! Thanks! Can you explain why I muust use '\\' instead '\' or can you give me link to documentatio which describe this? – user3764298 Mar 03 '20 at 13:14
  • Oracle Data Integrator also has a built-in sFTP tool that you can use in a package or a procedure without having to code it yourself. – JeromeFr Mar 03 '20 at 13:59

1 Answers1

1

You have to escape the backslashes in your local path String:

sftp.put("C:\\Tools\\tmp.html", "/var/temp/temp.html");

That's because a backslash itself escapes the next character and if you put single backslashes in "C:\Tools\tmp.html", the result would be (but isn't) something like "C:oolsmp.html", which is unlikely to be considered a valid path.

In my IDE (eclipse), a String with single backslashes is considered a compilation error, the code doesn't even compile.

The error is this:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    Invalid escape sequence (valid ones are  \b  \t  \n  \f  \r  \"  \'  \\ )
deHaar
  • 17,687
  • 10
  • 38
  • 51