Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java runtime exec with scp command

Tags:

java

This is a part of my code to copy files from local to a remote machine

try {
Process cpyFileLocal = Runtime.getRuntime().exec("scp  " + rFile+"*.csv"     + " root@" + host + ":" + lFile);
InputStream stderr = cpyFileLocal.getErrorStream();
InputStreamReader isr = new InputStreamReader(stderr);
BufferedReader br = new BufferedReader(isr);
String line = null;
System.out.println("<ERROR>");
while ((line = br.readLine()) != null) {
System.out.println(line);
}
System.out.println("</ERROR>");
int exitVal = cpyFileLocal.waitFor();
System.out.println("Process exitValue: " + exitVal);
System.out.println("...." + cpyFileLocal.exitValue());
System.out.println("SCP COMMAND  "+"scp "+rFile+"*.csv" +"  root@"+host+":"+lFile);
System.out.println("Sending  complete...");
} catch (Exception ex) {
ex.printStackTrace();
}

the output is...

<ERROR>

    /opt/jrms/rmsweb/transfer/cn00/outgoing/*.csv: No such file or directory
    </ERROR>

    Process exitValue: 1

    ....1

    SCP COMMAND  scp /opt/jrms/rmsweb/transfer/cn00/outgoing/*.csv [email protected]:/opt/jrms/transfer/incoming/

but when I run the command in terminal on the local machine, it works fine and when I run ll the files are there

-rwxr-xr-x 1 freddie freddie 140 Apr 22 09:13 gc00cn00150420092629.csv*

-rwxr-xr-x 1 freddie freddie 105 Apr 22 09:13 gc00cn00150420122656.csv*

Any help please

like image 723
Noli Godoy Avatar asked Apr 22 '15 03:04

Noli Godoy


2 Answers

If you are using java 7 and above you should use ProcessBuilder instead of Runtime.getRuntime().exec() and in the ProcessBuilder you can specipied the execution directory:

 ProcessBuilder pb = new ProcessBuilder("scp", rFile+"*.csv", "root@" + host + ":" + lFile);
 Map<String, String> env = pb.environment();
 env.put("VAR1", "myValue");
 env.remove("OTHERVAR");
 env.put("VAR2", env.get("VAR1") + "suffix");
 pb.directory("directory where the csv files located");
 Process p = pb.start();
like image 177
igreenfield Avatar answered Nov 19 '22 20:11

igreenfield


When you run command with in bash with wildcards like * in it, bash will expand that command and in your case, replaces *.csv with list of files terminating with .csv, but in your java program, this won't happen.

According to this answer, you can do following:

  • Use file.listFiles() to get the list of files
  • Use file.getName().contains(string) to filter them if needed
  • Iterate over the array and perform scp or do it with whole list

or with thanks to @James Anderson comment add sh before scp in your command.

like image 1
Mehraban Avatar answered Nov 19 '22 18:11

Mehraban