3

I am calling the below command using java

This is the java initialization

String fileName="C:\\temp\\A  a.txt";
String  sCmd = "cmd /c start \"\" \"" + fileName + "\"";

This is what I get when I print sCmd

 cmd /c start '" 'C:\temp\A   a.txt'

This is how I run the command

 Runtime.getRuntime().exec(sCmd);

The file name contains multiple spaces and when I run this command from Java its throwing an error because its not recognizing the multiple spaces.It works when no space or one space is there?How to handle files with multiple spaces through windows command

Sample java program

   import java.io.File;
import java.io.IOException;

public class A
{
    public static void main(String[] args)
    {
        String fileName = "C:\\temp\\a  dfdfd   f.txt";
        File file = new File(fileName);
        String sCmd = "cmd /c start \"\" \"" + file.getAbsolutePath() + "\"";

        System.out.println("exec cmd=<" + sCmd + ">");
        try
        {
            Runtime.getRuntime().exec(sCmd);
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

This gives me error when from windows side when I run the java code.

This is the java output

exec cmd=<cmd /c start "" "C:\temp\a  dfdfd   f.txt">

and I am running in Windows XP further this is not opening any file leave aside one with spaces.

Solution:

import java.io.File;
import java.io.IOException;

public class A
{
    public static void main(String[] args)
    {
        // String fileName = "C:\\temp\\a.txt";
        String fileName = "C:\\temp\\a  dfdfd   f.txt";
        File file = new File(fileName);
        String sCmd = "cmd /c start \"\" \"" + file.getAbsolutePath() + "\"";

        System.out.println("exec cmd=<" + sCmd + ">");
        try
        {
            Runtime.getRuntime().exec(sCmd.split(" "));
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
Harish
  • 3,343
  • 15
  • 54
  • 75
  • This is a known bug, try this answer it works very well for all situations, http://stackoverflow.com/a/19102375/3160597 – azerafati Apr 01 '14 at 06:38

2 Answers2

5

cmd /c start "C:\temp\A a.txt" works from the command line. You would need to escape the double quotes in the above command with a back slash if this command is called via Java's Runtime.getRuntime().exec(...)

qwerty
  • 3,801
  • 2
  • 28
  • 43
  • When I run the command cmd /c start "C:\temp\A a.txt" it opens the command prompt technically it should open the file. – Harish Apr 20 '11 at 04:45
  • 1
    Then i guess you're looking for cmd /c notepad "C:\temp\A a.txt" (to open it in notepad; you can use other applications too) – qwerty Apr 20 '11 at 04:48
  • running from command prompt right now is the issue.the main issue with the above standalone code.If you run it I get an windows error.I guess as you say the error is with " handling but I am not sure where is the error.Do you see any? – Harish Apr 20 '11 at 05:01
  • 1
    I have finally found the solution:If in future someone refers this they may use this code as placed above – Harish Apr 20 '11 at 08:04
1

I'm not exactly sure what you're doing but if it's simply cmd /c start <filename> then this has too quotes.

String  sCmd = "cmd /c start \"\" \"" + fileName + "\"";

It should be

String  sCmd = "cmd /c start \"" + fileName + "\"";

As an aside:

This is what I get when I print sCmd

cmd /c start '" 'C:\temp\A   a.txt'

I don't see how you could get single quotes in the output when they aren't present in your string!

no.good.at.coding
  • 20,221
  • 2
  • 60
  • 51