Java exec( "cp x y" )...

This is a discussion on Java exec( "cp x y" )... within the Tech Board forums, part of the Community Boards category; OK, this is crazy... If I run this command on the command line it works fine: Code: cp -r /testing/schema_xmls/simple_schema* ...

  1. #1
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545

    Java exec( "cp x y" )...

    OK, this is crazy...
    If I run this command on the command line it works fine:
    Code:
    cp -r /testing/schema_xmls/simple_schema* /var/schemas
    but if I run the exact same command from a Java program with the exec() function it gives me:
    Code:
    cannot stat `/testing/schema_xmls/simple_schema*': No such file or directory
    WTF??
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    I doubt exec() does wildcard expansion on '*'. It probably doesn't even go through a shell. Note that your '*' is being taken as a literal '*', what do you expect exec() to do with it?

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by zacs7 View Post
    I doubt exec() does wildcard expansion on '*'. It probably doesn't even go through a shell. Note that your '*' is being taken as a literal '*', what do you expect exec() to do with it?
    Yeah, I just realized that.
    So how would I run it with the wildcard then?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,579
    Wait, I know this one. (I'm actually going to reply to a java question. Where's my cookie?)
    With java exec() you need to call the shell. You script your command, and then exec(your_script). Note that for all purposes you can create the script on the fly, execute it, and delete it. No need for any menial work.

    I had to do this sometime as part of my Linux learning process. Some book or another used a few java examples. Go figure why.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You've got to be kidding... Isn't there some way to do something like this:
    Code:
    exec( "bash <some option> cp -r foo/* bar" );
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,579
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #7
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,690
    Code:
    /bin/sh -c "cp -r /testing/schema_xmls/simple_schema* /var/schemas"
    is what you're looking for, I believe.
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Yarin View Post
    Code:
    /bin/sh -c "cp -r /testing/schema_xmls/simple_schema* /var/schemas"
    is what you're looking for, I believe.
    Yes, it looks like the -c option should work. I'll try it tomorrow.
    Thanks.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Yarin View Post
    Code:
    /bin/sh -c "cp -r /testing/schema_xmls/simple_schema* /var/schemas"
    is what you're looking for, I believe.
    Damn. That didn't work either.
    Now it says:
    Code:
    -r: -c: line 0: unexpected EOF while looking for matching `"'
    -r: -c: line 1: syntax error: unexpected end of file
    Exception in thread "main" java.lang.RuntimeException: Exit code 2 was returned while running: "/bin/bash -c "cp -r /testing/schema_xmls/simple_schema* /var/schemas""
    I'm going to switch to a different method that doesn't require me to copy sub-directories, so I won't need the *, but it would still be nice to find out how to do this for the next time I run into it.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  10. #10
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    >unexpected EOF while looking for matching `"'

    Are you sure you had the closing quote in the command?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C#, Java, C++
    By incognito in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 10-05-2004, 03:06 PM
  2. The Java language is being expanded
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 06-11-2004, 10:07 PM
  3. Java woes
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 07-06-2003, 01:37 AM
  4. How to use Java with C++
    By Arrow Mk 84 in forum C++ Programming
    Replies: 2
    Last Post: 02-27-2003, 04:12 PM
  5. Visual J#
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 10-08-2001, 03:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21