Question

I am using below code to run SFTP command through Jsch:

public void putfile(){

    try {                      
        String command="winscp /script=D:\\command.txt" ;
        System.out.println(command);
        Runtime rt=Runtime.getRuntime();

        rt.exec(command);
    }
    catch(Exception e) {
        System.out.println("Exception in index.jsp:"+e.getMessage());
    }
}

I am putting sample.zip from local machine to Unix Server,

The command.txt contains:

option confirm off

open sftp:User:password@IP

get G:\sample.zip /MyFolderLocation/

exit

Its working fine but i am not getting any exception whenever the SFTP process fails.Is there any way to handle it?

Was it helpful?

Solution

There are two common ways to detect whether a subprocess you ran encountered an error:

  1. check its exit code,
  2. see if it writes any error messages to standard output or standard error.

I don't know whether WinSCP sets an exit code in the event of an error, or writes a message to standard output or standard error, but it should do at least one of them. (It may even do both, which is what I'd expect all well-behaved processes to do.) You'll just have to try out WinSCP and see what happens.

I'd also recommend using a ProcessBuilder instead of Runtime.getRuntime().exec(...) to run the command. If nothing else, this allows you to redirect standard error into standard output so you only have one stream to read from.

Here's what your code looks like after I've modified it to use a ProcessBuilder, get the process's exit code and read the output from the command into a string:

public void putfile() {

    try {                      
        ProcessBuilder builder = new ProcessBuilder("winscp", "/script=D:\\command.txt");
        builder.redirectErrorStream(true);

        Process process = builder.start();

        // read output from the process
        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        StringBuilder outputBuilder = new StringBuilder();
        String line;
        do {
           line = reader.readLine();
           if (line != null) { outputBuilder.append(line).append('\n'); }
        } while (line != null);

        reader.close();

        String output = outputBuilder.toString();
        // inspect output for error messages.

        int exitCode = process.waitFor();
        // see if exit code is 0 (success) or != 0 (error)
    }
    catch(Exception e) {
        System.out.println("Exception in index.jsp:"+e.getMessage());
    }
}

Of course, reader should be closed in a finally block. For clarity I haven't bothered doing this in the code above.

Note that when running a subprocess in Java you should always read the subprocess's output, even if you don't care about its contents. If you don't, the buffer that the output gets written into may fill up, which will cause the subprocess to hang if it wants to write any more.

OTHER TIPS

See WinSCP FAQ How do I know that script completed successfully?

You can tell the result of script by WinSCP exit code. Code 0 indicates success, while 1 indicates an error. For more details refer to scripting documentation.

Batch script (specified using /script or /command command-line switches) terminates with code 1 immediately once any error occurs.

To find out why the script failed, inspect session log.

For an example see guide to transfer automation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top