10 Replies Latest reply on Oct 20, 2009 9:27 PM by 807580

    Spaces in directory name?

    807580
      Does anyone know how to run a command with a space or spaces in the directory name?

              try {
      
              Runtime.getRuntime().exec("cmd /c \"C:\\TEST\\my file is here\\setup.exe");
      
              } catch (IOException ex) {
      
              }
      
      {code}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
        • 1. Re: Spaces in directory name?
          807580
          Wrap it by quotes.
          • 2. Re: Spaces in directory name?
            796440
            Try escaping double quotes into the string
            "\"C:\\a b c\""
            Or use the exec signature that takes args as separate parameters.

            Or use ProcessBuilder.
            • 3. Re: Spaces in directory name?
              807580
              Well, the directory name will be a variable.
              Object mmmsd = jComboBox2.getSelectedItem();
              
                      try {
               
                      Runtime.getRuntime().exec("cmd /c \"C:\\TEST\\" + mmmsd + "\\setup.exe");
               
                      } catch (IOException ex) {
               
                      }{code}
              
              
              I tried doing this but it didn't work.
              {code}
              Object mmmsd = jComboBox2.getSelectedItem();
              
                      try {
               
                      Runtime.getRuntime().exec("cmd /c \"'C:\\TEST\\" + mmmsd + "\\setup.exe'");
               
                      } catch (IOException ex) {
               
                      }
              Edited by: IYIaster on Oct 20, 2009 10:27 AM

              Edited by: IYIaster on Oct 20, 2009 10:29 AM
              • 4. Re: Spaces in directory name?
                796440
                Then try one of the other options I suggested.

                Or print out
                "cmd /c \"C:\\TEST\\" + mmmsd + \\setup.exe"
                and see what it looks like.
                If it's not of the form
                cmd /c "C:\TEST\a b\c d\setup.exe"
                then you're building your string wrong.

                If it is of that form, then you'll need to be more specific about what's going wrong.
                • 5. Re: Spaces in directory name?
                  807580
                  You normally start and end a string with ".
                  If you want to represent a quote in a string, use \".

                  So
                  "cmd /c \"C:\\TEST\\" + mmmsd + "\\setup.exe"
                  should have been
                  "cmd /c \"C:\\TEST\"" + mmmsd + "\\setup.exe"
                  Do you for instance use a code editor with syntax highlighting? It helps a lot.

                  It helps also a lot if you tell in detail what exactly didn't work. There's a huge difference between a compile error and runtime error. A compile error is a plain syntax error (a developer fault).
                  • 6. Re: Spaces in directory name?
                    807580
                    BalusC wrote:
                    You normally start and end a string with ".
                    If you want to represent a quote in a string, use \".

                    So
                    "cmd /c \"C:\\TEST\\" + mmmsd + "\\setup.exe"
                    should have been
                    "cmd /c \"C:\\TEST\"" + mmmsd + "\\setup.exe"
                    I thought it should have been
                    "cmd /c \"C:\\TEST\\" + mmmsd + "\\setup.exe\""
                    • 7. Re: Spaces in directory name?
                      807580
                      Oh yes, you're right.
                      • 8. Re: Spaces in directory name?
                        807580
                        "cmd /c \"C:\\TEST\\" + mmmsd + "\\setup.exe\""
                        That worked thanks!
                        • 9. Re: Spaces in directory name?
                          796440
                          Did you try my suggestion to print out what you were actually sending to exec(), so that you could maybe see for yourself if it looked right?
                          • 10. Re: Spaces in directory name?
                            807580
                            I did and it didn't look right. But, then I tried
                            Runtime.getRuntime().exec("cmd /c \"C:\\TEST\\" + mmmsd + "\\setup.exe\"");
                            and it printed out right. But I didn't change the runtime scripts so it still didn't work. But, after I read the forums and went back and looked at it I realized what I did.