Results 1 to 6 of 6
  1. #1
    divs1210 is offline Member
    Join Date
    Apr 2011
    Posts
    16
    Rep Power
    0

    Exclamation Error While Running Windows Command Line Instruction

    I am running the following code to open a file using the default application in Windows:

    Java Code:
    String s1 = jfilechooser.getSelectedFile().getPath();
    Runtime rt = Runtime.getRuntime();
    Process proc = rt.exec("cmd /c start "+s1);
    The problem is:

    if the file/directory name contains a space, it does not give the full name at the command line, even though it works PERFECTLY for files/folders whose names do not contain spaces.
    eg:
    if i select "Hello.txt", it works fine.
    but if i select "Bye Dude.txt", Windows gives me an error saying:

    "Bye" could not be found.

    What should i do to solve this? PLEASE HELP!!!

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default

    Can you add quotes around s1?

    To have quotation marks in a String, you have to escape them: "\"I am in quotation marks!\""
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    divs1210 is offline Member
    Join Date
    Apr 2011
    Posts
    16
    Rep Power
    0

    Default

    thank you, Kevin.

    got a solution:

    Java Code:
    String fileName = jfilechooser.getSelectedFile().getPath();
                String[] commands = {"cmd", "/c", "start", "\"Watever\"",fileName};
                Runtime.getRuntime().exec(commands);

  4. #4
    divs1210 is offline Member
    Join Date
    Apr 2011
    Posts
    16
    Rep Power
    0

    Default

    'watever' is just a random title that i've given to it.

    thanks for your help!

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default

    Quote Originally Posted by divs1210 View Post
    thank you, Kevin.

    got a solution:

    Java Code:
    String fileName = jfilechooser.getSelectedFile().getPath();
                String[] commands = {"cmd", "/c", "start", "\"Watever\"",fileName};
                Runtime.getRuntime().exec(commands);
    Glad you got it working. For the record, I was thinking something like this:

    Java Code:
    Process proc = rt.exec("cmd /c start "+ "\"" + s1 + "\"");
    I haven't tested it at all.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    to_devendra is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    Whenever any space occur in filename or directory path, we must use quote surround with the input string. That always be a good practise thus "expected input string".

    Below one will work perfectly.
    Process proc = rt.exec("cmd /c start "+ "\"" + s1 + "\"");

Similar Threads

  1. Running Jar file using command line through Java Codes
    By NaturalE in forum Advanced Java
    Replies: 2
    Last Post: 02-24-2011, 04:07 AM
  2. Java command line Windows vs Linux
    By shahrukh in forum New To Java
    Replies: 2
    Last Post: 01-16-2011, 05:52 PM
  3. Command Line Error
    By Differintegral in forum New To Java
    Replies: 8
    Last Post: 07-29-2010, 09:49 AM
  4. Replies: 2
    Last Post: 03-31-2010, 12:26 AM
  5. Replies: 1
    Last Post: 06-30-2009, 02:32 PM

Tags for this Thread

Posting Permissions

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