DMM DMM - 1 year ago 87
Java Question

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.

Answer Source

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

String args[] = {
    "C:/Logfiles/Client_Logfiles/Attachments/1044/image2   Copy.jpg"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download