Find the answer to your Linux question:
Results 1 to 6 of 6
I build 2 separate groups of cluster boxes. They suppose to be exactly the same but seems like they are not. we have a java app that calls cp system ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jul 2008
    Posts
    2

    cp not working


    I build 2 separate groups of cluster boxes. They suppose to be exactly the same but seems like they are not.

    we have a java app that calls cp system call:

    Process p = Runtime.getRuntime().exec("\bin\cp \""+source-path+" "+destination-path+"\"");

    This app runs ok on one group of cluster but fails in the 2nd group with the following error:

    "java.io.IOException: Error Stream: /bin/cp: cannot stat ..."

    what could be the culprit?

    I checked every possible configurations I could think of, i.e. path, cp version, mount points, file system permissions, etc..., and they are exactly the same between the groups.
    tia

  2. #2
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    What is the full error message? I usually see errors like that when either the source or the destination doesn't exist / can't be created.

  3. #3
    Just Joined!
    Join Date
    Jul 2008
    Posts
    2
    i don't think the availability/permission of source + destination is the problem since if i take the double quotes out from the call as such:

    Process p = Runtime.getRuntime().exec("\bin\cp "+source-path+" "+destination-path);

    it will work. I need the quotes in the call to compensate for file names/paths with spaces.

  4. #4
    Linux Engineer Thrillhouse's Avatar
    Join Date
    Jun 2006
    Location
    Arlington, VA, USA
    Posts
    1,377
    How is this code even compiling? "\bin\cp" is interpreted by the Java compiler differently than I think you're expecting it to be. Slash-anything is considered an escape character and neither \b nor \c are considered valid escape characters. The compiler should catch that. "/bin/cp" would be considered the appropriate system call. The other thing is you can't have dashes in variable names in Java. The compiler will catch that too and will interpret "source" and "path" as two different variables.

  5. #5
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    Well, I'm not 100% certain about Java, but "\b" usually means backspace (try running the command: echo "a\bc"). I'm not familiar with "\c", but for all I know, that could be something obscure.

    I didn't catch that error because Java will usually freely translate between \ and / as far as paths are concerned, but you're correct: you usually have to escape them.

    Looking at your code again (with corrections to eliminate other possible problems, I notice this:
    Code:
    /bin/cp \""+source_path+" "+destination_path+"\"
    Let's imagine that the command gets constructed by filling in the paths:
    Code:
    /bin/cp "source_file dest_file"
    Notice how both arguments are grouped into a single argument by the quotes. Try quoting each argument separately and see if that works.

  6. #6
    Linux Engineer Thrillhouse's Avatar
    Join Date
    Jun 2006
    Location
    Arlington, VA, USA
    Posts
    1,377
    You're right. Java does allow \b, but not \c. I tested with this simple class:
    Code:
    public class Test {
        public static void main(String[] args) {
            String source-path = "/home/me/test.sh";
            String destination-path = "/home/me/scripts/test.sh";
            try {
                Process p = Runtime.getRuntime().exec("\bin\cp \""+source-path+" "+destination-path+"\"");
            } catch (Exception e) {System.out.println(e.getMessage());}
        }
    }
    and it complained about the variable declarations and about an illegal escape character. Change it to this:
    Code:
    public class Test {
        public static void main(String[] args) {
            String source_path = "/home/me/test.sh";
            String destination_path = "/home/me/scripts/test.sh";
            try {
                Process p = Runtime.getRuntime().exec("/bin/cp \""+source_path+" "+destination_path+"\"");
            } catch (Exception e) {System.out.println(e.getMessage());}
        }
    }
    and it compiles fine. But obviously, there's no expectation that the OP's code would work, let alone compile.

    I would do without the escaped quotes and use String's replace method to find all spaces in your directory names and replace them with an escaped space sequence.

    EDIT: BTW, Cabhan, I just noticed you are now a Trusted Penguin. Kudos! A well-earned designation.

    One last thing: whenever you exec() something, you should be sure to consume the child process' entire input stream. I don't know if you are doing that or not but I'll mention it just in case.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •