• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

File cannot open when it has double spaces in file name

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 java:(This 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.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This will tie you down to the Windoze platform.
The recommended way would be Desktop#open(File fileToOpen);
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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().
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic