0

I have a Java Runtime.getRuntime().exec() problem. I run my java app on Linux and just need to execute a basic task: copy and rename a file using cp command. However, it doesn't seem to work.

This is my chunk of code to call the Runtime.getRuntime().exec():

String command = "cp -f '" + old_path + "' " + song_info[6] + ".mp3";                
System.out.println(command);
log.info(command);
            try{
                p = Runtime.getRuntime().exec(command);
                int returnCode;
                try {
                    returnCode = p.waitFor();
                    System.out.println("Return code = " + returnCode);
                } catch (InterruptedException ex) {
                    java.util.logging.Logger.getLogger(Import.class.getName()).log(Level.SEVERE, null, ex);
                }

            } catch(IOException e){log.error(e);}

When I run the java app the command for each loop is something like this

cp -f '/temp_storage/LoveSongs/28.I miss you.mp3' /music_storage/data/0/0/1/108.mp3

If I copy that log line and run it in the command line, it works perfectly. However the result from java app always return code 1 (which indicate failure). Even including /bin/bash -c before the command string, it still doesn't work.

Any suggestion why? I've just install JRE and JDK on that server. When I type java -version I got:

java version "1.6.0_17" OpenJDK Runtime Environment (IcedTea6 1.7.10) (rhel-1.20.b17.el5-x86_64) OpenJDK 64-Bit Server VM (build 14.0-b16, mixed mode)

skaffman
  • 398,947
  • 96
  • 818
  • 769
Jim Raynor
  • 2,268
  • 3
  • 31
  • 36
  • P/S: It didn't throw any sort of Exception. – Jim Raynor Apr 21 '11 at 09:57
  • have yout tried running this on any other O/S or just Linux? – My Head Hurts Apr 21 '11 at 10:04
  • @My Hed Hurts: I ran the similar task, calling the shell script file on another server and it runs normally. Only problem with this one. – Jim Raynor Apr 21 '11 at 10:27
  • 1
    Perhaps this was just a simple example of something more complicated you are trying to accomplish, but why would you fork a process to copy a file? It is relatively simple to do directly in java. The apache commons-io project even has [utility methods](http://commons.apache.org/proper/commons-io/javadocs/api-2.4/org/apache/commons/io/FileUtils.html#copyFile(java.io.File,%20java.io.File)) to make it even easier. – Brett Okken Dec 06 '13 at 14:00

2 Answers2

4

AFAIK, if you command has multiple command line parameters you need to supply them in a String[], have you tried that? Furthermore if you need to copy files around you can use FileUtils or Guava libraries (dont remember what it's called there)

see this relevant thread about copying files

Community
  • 1
  • 1
posdef
  • 6,498
  • 11
  • 46
  • 94
  • this is correct -- the system will try to find an executable called 'cp -f ...' rather than find an executable called 'cp' to which it passes the parameters '-f ...' – Liv Apr 21 '11 at 12:36
1

Does the user that the application is running under have the necessary permissions? And from the process, have you tried getting hold of stderr to see if there is anything useful in there?

DaveH
  • 7,187
  • 5
  • 32
  • 53