• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SFTP from java

 
selvakumar Thiyagarajan
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:


Error
cd /temp/ cannot open

 
Paul Clapham
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ftwssfg.aur.com" "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
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul,
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



TestScript.sh

cd /temp/
ftp -n ftwssfg.aur.com
quote user amwkui
quote pass amwkui
cd /home/data/
put todayFile.txt
quit
exit

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/TestScript.sh" );

I got the error below error,
web-inf/config/dev/TestScript.sh: 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
Pie
Posts: 18277
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
selvakumar Thiyagarajan
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Tim. It worked after giving the absolute path as parameter.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic