javaunixputtypscp

How can I traverse to parent directory when calling PSCP using Java's ProcessBuilder?


I'm calling pscp from Java using the following code:

public static ArrayList<String> runWindowsCommand(String... args) throws WindowsCmdFault {
        try {
              ProcessBuilder pb = new ProcessBuilder(args);
              Process p = pb.start();


              //...
              //... code to fetch the output and return it back


            }catch (IOException | InterruptedException e) {
              e.printStackTrace();
            }

    }

I call this function as follows:

runWindowsCommand("C:\Program Files (x86)\PuTTY\pscp" -pw "password" -r folder/file_to_be_transferred.txt "username@hostname:/remote/unix/server/location/folder_name")

This executes fine and shows the following output:

Arraylist returned (output) = 
[, file_to_be_transferred.txt                | 3 kB |   2.7 kB/s | ETA: 00:00:00 | 100%]

It appears that the transfer was successful. Except that when I login to the remote unix server via putty and check that file, it shows that nothing was updated.

The file's permission has been set to 666.

When I execute this same command (that we're sending to the function) directly on the cmd prompt, then it gives the same output and the file is actually transferred.

When I run this same command via the Java code given above the file isn't actually transferred.

Why is PSCP not transferring the file?

Update:On further investigation and a big thanks to @Martin Prikryl, I was able to narrow down this issue to the root cause.

Root cause: The problem is that when the user connection is established from the PSCP, then it connects by default to (root)/home/username .

The directory that we need to traverse to lies in

(root)/www/....lengthy/folder/here .

Remaining problem:

I'm trying to traverse upwards through the folders after connecting. I'm trying to add a double-period in order traverse upwards towards the root before supplying my path. But it doesn't work.

I'm trying this with pscp as the remote hostname:

../../www/..lengthy/folder/here 

but this fails for some odd reason when executed through Java but works through cmd prompt.

How can I add a remote path that includes the second parent before including my actual path?


Solution

  • It seems like there's some limitation when you use Java + PSCP. I solved this problem by uploading the file to a default directory. After that I make a connection via putty's command-line and transfer the file to the required destination. I'm placing my answer over here.