1

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

2
  • It seems the execChannel has no notion of "current directory", so you probably need to specify the full absolute path to process.ksh. If this has specific requirements about the current directory, it should set the directory itself. epaul.github.io/jsch-documentation/javadoc/com/jcraft/jsch/… Commented Jan 27, 2016 at 22:40
  • if i use the absolute path of ksh file and try to execute , it returns me another error, because argument file is not there. to make it simple, i have the script in Script directory and data in Data directory. I want to run the ksh program in data directory with the argument which is not happening. I was able to change the directory with appended command still no luck. is there any special command to run ksh program ? Commented Jan 28, 2016 at 16:15

2 Answers 2

1

"sftp" channel are not made to execute shell command but sftp command only.

    Channel channel = session.openChannel("shell");
    cmdSend = channel.getOutputStream();
    InputStream cmdRcv = channel.getInputStream();
    // Start a Thread reading and displaying cmdRcv

    channel.connect(3000);
    Thread.sleep(1000);

    cmdSend.write("cd /to/the/right/dir\n".getBytes());
    cmdSend.flush();
    cmdSend.write("sh process.ksh \"a.txt\"\n".getBytes());
    cmdSend.flush();
Sign up to request clarification or add additional context in comments.

3 Comments

looks like it doesn't do anything, how do i print error/success messages. Any suggestion pls.
I was able to print out . output came like that :Interactive login with a GenericID is prohibited. You need to re-authenticate with your ID before continuing. Enter your ID: But i was successfully connected to that server. If i need to put credentials again how do i put that. appreciate your response. Nur
I guess this means : you try to execute command on a server where the administrator prevents you from executing command.
1

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:

/bin/sh /path/to/bin/process.ksh /path/to/data/ "a.txt"

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:

cd $1
shift

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.

2 Comments

did not get the last part. can you share the full example please
@nur, not much more to add to the example, but I have added some more explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.