Search code examples
javawindowsexecspacesrundll32

File cannot open when it has double spaces in file name


I am trying to open image using java program which file name has more than once spaces. direct windows command is working fine, but when I execute via java program it is not opening.

direct command :

rundll32.exe shell32.dll ShellExec_RunDLL "C:\Logfiles\Client_Logfiles\Attachments\1044\image2   Copy.jpg"

via java:(This is not working)

p_fileName = "C:\Logfiles\Client_Logfiles\Attachments\1044\image2   Copy.jpg"
String cmd = "rundll32.exe shell32.dll ShellExec_RunDLL ";
Runtime.getRuntime().exec(cmd + "\""+p_fileName+"\"");

But if file name has one space, it is fine and opening properly:

Please any ideas on this and appreciate your kind help.


Solution

  • The problem is you concatenate command. To make it correct use array version of exec and use '/' instead of '\':

    String args[] = {
        "rundll32.exe",
        "shell32.dll",
        "ShellExec_RunDLL",
        "C:/Logfiles/Client_Logfiles/Attachments/1044/image2   Copy.jpg"
    };
    Runtime.getRuntime().exec(args);