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 ...
- 09-16-2008 #1Just 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
- 09-17-2008 #2
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.
DISTRO=Arch
Registered Linux User #388732
- 09-17-2008 #3Just 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.
- 09-17-2008 #4
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.
- 09-18-2008 #5
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:
Let's imagine that the command gets constructed by filling in the paths:Code:/bin/cp \""+source_path+" "+destination_path+"\"
Notice how both arguments are grouped into a single argument by the quotes. Try quoting each argument separately and see if that works.Code:/bin/cp "source_file dest_file"
DISTRO=Arch
Registered Linux User #388732
- 09-18-2008 #6
You're right. Java does allow \b, but not \c. I tested with this simple class:
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.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());} } }
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.


Reply With Quote
