I am trying to run a program which process a file from window using jcraft library in Unix. what i found after establishing the channel it always try to run the program in home directory but i need to run in a separate directory.please have a look what i tried so far and let me know what i am missing.
String strRemoteDir = "/home/process/input" channel = session.openChannel("sftp");
channel.connect();
System.out.println("sftp channel opened and connected.");
channelSftp = (ChannelSftp) channel;
// Printing Home Directory in Unix Server
System.out.println(channelSftp.getHome());
channelSftp.cd(strRemoteDir);
System.out.println(channelSftp.pwd());
// for uploading a file where i need to run the program
File f = new File(fileName);
channelSftp.put(new FileInputStream(f), f.getName());
System.out.println("File transfered successfully to host.");
fileTransfer = true;
channel=session.openChannel("exec");
InputStream in=channel.getInputStream();
// it is printing the desired directory where i want to go
System.out.println(channelSftp.pwd());
((ChannelExec)channel).setCommand("sh process.ksh "a.txt");
channel.setInputStream(null);
((ChannelExec)channel).setErrStream(System.err);
channel.connect();
output : process.ksh not found
But through putty i was able to run the program. just to let you know process.ksh is not in the input directory, but has the capability to run from anywhere with arguments. ((ChannelExec)channel).setcommand("ls") prints out all the files from home directory. i believe i am establishing a channel to home directory, i just don't know how to run bash program using jcraft in a desired location.Please let me know what i am missing or is it possible to make it happen.
Thanks in advance. Nur
"sftp" channel are not made to execute shell command but sftp command only.
Others have provided examples of how to set the working directory. However, a more defensive style of programming is to assume nothing and specify everything explicitly. So for specifying the command to execute, something like:
Notice that I have added a new first argument to your command, the directory you want to run it from.
Now, change your script so that it changes to this directory (given as parameter $1) before continuing.
This can be done by adding
cd
at the beginning, something like:The
shift
command shifts all other arguments, so that $2 becomes $1 and so on, so that the rest of the script will find the arguments where it expects.