Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

File cannot open when it has double spaces in file name

 
Dilanka Muthukumarana
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 :


via javaThis is not working)



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

Please any ideas on this and appreciate your kind help.
 
Maneesh Godbole
Saloon Keeper
Posts: 11329
16
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This will tie you down to the Windoze platform.
The recommended way would be Desktop#open(File fileToOpen);
 
Stephan van Hulst
Bartender
Pie
Posts: 6503
83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The exec() command that takes a command string breaks the string up in tokens. That means the command array will contain the following elements:

There are two things wrong with this. Firstly, obviously your file name is broken up in two tokens, rather than one, and secondly, the slashes in your String are not uses as slashes but to escape the next character.

1. Properly escape the slashes in your path. It's best to use Paths.get() to build the path and then call Path.toString().
2. Call the exec() overload that takes an array as its first argument.

An example on how to construct path strings properly:
 
Stephan van Hulst
Bartender
Pie
Posts: 6503
83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you go with Maneesh's recommended approach, don't create a File from a String directly, but create a Path like I showed you, and then call Path.toFile().
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic