aspose file tools
The moose likes Linux / UNIX and the fly likes SFTP from java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "SFTP from java" Watch "SFTP from java" New topic

SFTP from java

selvakumar Thiyagarajan

Joined: Oct 06, 2008
Posts: 28
I have a requirement to FTP text files to a remote machine(unix) from my application. My application runs on unix(solaris) box. I couldnt use any open source libraries to SFTP files. So, i thought of doing this with set of unix commands and executing that from java. I am using process builder API to execute the commands.
Whenever i try to execute the unix command, i am getting errors.
I am new to scripts and i am using RAD IDE. Please bear with me if there are any silly mistakes.
My code snippet:

cd /temp/ cannot open

Paul Clapham

Joined: Oct 14, 2005
Posts: 19129

You seem to have misunderstood ProcessBuilder, or shell scripts, or quite likely both.

The parameters you pass to ProcessBuilder are the name of an executable followed by the parameters for that executable. So the command you were trying to execute looks something like this:

sh "cd /temp/" "ftp -n" "quote user amwkui" ...

Obviously that isn't a script.

Also you included data in the script that the FTP command should be reading from somewhere. The script doesn't act as stdin for the programs which it runs. And of course if you're really using SFTP then that plain old FTP command isn't going to work either.
selvakumar Thiyagarajan

Joined: Oct 06, 2008
Posts: 28
Thanks for the advice. Now i added the scripts in a separate file. when i execute the script as standalone, it is FTPing the file as expected. But I am facing problems while executing it through the ProcessBuilder API. shell script is placed in the web-inf/config/dev folder

cd /temp/
ftp -n
quote user amwkui
quote pass amwkui
cd /home/data/
put todayFile.txt

Below is error i am getting when i run the above piece,

/usr/bin/bash: No such file or directory

Also when i changed the parameters to processbuilder class as below,

ProcessBuilder pb = new ProcessBuilder("sh", "web-inf/config/dev/" );

I got the error below error,
web-inf/config/dev/ Cannot open

I am not sure about the working directory. Is it the directory where the script is placed?

Please suggest on this.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16395

When using ProcessBuilder or Runtime.exec(), it's always better if you use the absolute path to the executable, not a relative path.

J2EE is based on WARs, which are specially-formatted ZIP files. So the "directories" in a WAR are not guaranteed to correspond to physical disk directories, and when they don't, the command shell won't be able to access them. Also, for all practical purposes, there is no "current directory" in a webapp. The process current directory is, for all intents and purposes, both unknown and subject to change without notice.

Because of this, any attempt to execute a shell script in a WAR-relative location cannot be guaranteed to succeed. It's better to place the script somewhere outside of the webapp in the regular filesystem. /usr/local/bin is a good option for Linux.

Having said that, you actually can keep the script inside the WAR as long as you are willing to take some risks. Many webapp servers will explode (unzip) a WAR when it is deployed, which means that the WAR directories will be real directories and the script file will be a real, independent file. There's a ServletRequest method that can be used to obtain the absolute filesystem path of WAR resources in such cases. Just be aware that if someone turns the explode option off or a new server version comes out that doesn't explode WARs, it will return null and you won't be able to use the script.

Customer surveys are for companies who didn't pay proper attention to begin with.
selvakumar Thiyagarajan

Joined: Oct 06, 2008
Posts: 28
Thanks Tim. It worked after giving the absolute path as parameter.
I agree. Here's the link:
subject: SFTP from java