6

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 root@10.50.1.29:/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

Noli Godoy
  • 61
  • 1
  • 2

3 Answers3

2

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.

Community
  • 1
  • 1
Mehraban
  • 3,164
  • 4
  • 37
  • 60
  • the weird thing is using the same command, copying files from remote to my local machine *.csv works fine, however, using *.csv from Local to remote, that's when the issue occur. Ant ideas? thanks – Noli Godoy Apr 22 '15 at 04:28
  • It's because scp will interpret that command at remote server (and probably in a bash), and list of files will be expanded on that host, not locally. But when you try local to remote file exchange, there is no bash to expand it. – Mehraban Apr 22 '15 at 04:32
  • 1
    Or you could just add an "sh" shell command before the " scp " – James Anderson Apr 22 '15 at 05:39
  • @SAM thanks for your inputs..seems to be working now – Noli Godoy Apr 22 '15 at 05:52
2

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();
igreenfield
  • 1,618
  • 19
  • 36
0

According to this, you should try:

Process cpyFileLocal = Runtime.getRuntime().exec(new String[] {"/bin/sh","-c", "scp  " + rFile+"*.csv"     + " root@" + host + ":" + lFile});

I tested with /bin/sh and /bin/bash, both copied the files over successfully

Ian2thedv
  • 2,691
  • 2
  • 26
  • 47