Results 1 to 12 of 12
  1. #1
    nhabibi is offline Member
    Join Date
    Jul 2008
    Posts
    5
    Rep Power
    0

    Default Problem with Runtime.exec()

    Hi all,

    I want to get a list of a directory with dir command (windows Vista). I write:

    Runtime.getRuntime.exec("cmd.exe /C dir " + MyPath);

    but the command can't find the specified path for dir. My question is that how we can pass an argument for command line's programs like dir, copy,etc?

    :confused:

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  3. #3
    nhabibi is offline Member
    Join Date
    Jul 2008
    Posts
    5
    Rep Power
    0

    Default

    Yes, actually it is:
    Runtime.getRuntime().exec().
    I made a mistake in posting.
    What is the problem?

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Why don't you just use File?

    But, did you use the full path? Or did you use a partial path, in which case, your "current path" is probably not what you thought it was.

  5. #5
    nhabibi is offline Member
    Join Date
    Jul 2008
    Posts
    5
    Rep Power
    0

    Default

    I ue the full path. I know that I can do it with File, but I want to know how to do with exec().

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Well, it might help to show the actual code, but probably better is this:

    When Runtime.exec() won't - Java World

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  8. #8
    nhabibi is offline Member
    Join Date
    Jul 2008
    Posts
    5
    Rep Power
    0

    Default

    no exception

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Did you check the output is correct, trying to write into a stream? And also the best way is, use a command string array. Check overloaded functions of exec().

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default

    What happens if you leave off the cmd.exe /C and start the comand with dir?
    Have you tried the exec() with other commands and gotten them to work?

  11. #11
    nhabibi is offline Member
    Join Date
    Jul 2008
    Posts
    5
    Rep Power
    0

    Default

    It solved. If a program generates output or error, you should get the stream and write it immediately.

    I read the javaworld article, thanks Masijade :)

  12. #12
    scotter59 is offline Member
    Join Date
    Jun 2008
    Posts
    13
    Rep Power
    0

    Default

    Quote Originally Posted by nhabibi View Post
    Hi all,

    I want to get a list of a directory with dir command (windows Vista). I write:

    Runtime.getRuntime.exec("cmd.exe /C dir " + MyPath);

    but the command can't find the specified path for dir. My question is that how we can pass an argument for command line's programs like dir, copy,etc?

    :confused:
    Might need to have something like this ("cmd.exe /C dir """ + MyPath + """") had a similar issue with vbscript today, damn """" and spaces. This may not be exact but might help with your problem.

Similar Threads

  1. Runtime.exec(), handling input and output streams
    By crookshank in forum New To Java
    Replies: 0
    Last Post: 06-05-2008, 02:41 PM
  2. Example of RunTime class
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-04-2008, 09:32 AM
  3. Exec cmd from Java application
    By javaplus in forum New To Java
    Replies: 0
    Last Post: 12-17-2007, 08:41 AM
  4. runtime method
    By roo7 in forum Advanced Java
    Replies: 5
    Last Post: 11-30-2007, 04:35 AM

Posting Permissions

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