• 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

Using Runtime to open a file with parentheses in the name

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public void openFile() throws IOException{
String[] openIt = new String[3];
openIt[0] = "cmd";
openIt[1] = "/c";
openIt[2] = getFile();
Runtime p = Runtime.getRuntime();
p.exec(openIt);
}

The above code is used to open a file. The method getFile() returns a string that is a file pathname such as "F:\Files\https___ecf.ord.uscourts.gov_cgi-bin_show_temp.pl_file=91062-2882988-0--headed_LM.pdf" or "C:\Working File\Dummy List3) 7-3-07 - 5-30-08.pdf".

Neither of these files will open with the above code. Any suggestions?

Thank you.

P.S. I know about backslashes being an issue. I have a StringTokenizer already fixing that problem. I figured a StringTokenizer might work for this problem, but I'm not sure how I would implement it in this case (special characters as delimiters, "\" to escape them?)
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take the command which you propose to execute via Java, and go to the Windows command line and see if you can execute that there, directly. If you can't do it at the command line then you can't expect Java to do any better.

And if you can't do it, check out the Windows help to see how you would have to modify the command to make it run.
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's an issue of cmd.exe - it simply does not handle spaces in the command well.

But why don't you check out java.awt.Desktop#open(java.io.File).
 
Jesse Hayes
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your ideas. The files open from command line. I had a problem with spaces but I got over it by putting the command in an array.

The example filenames I gave, "F:\Files\https___ecf.ord.uscourts.gov_cgi-bin_show_temp.pl_file=91062-2882988-0--headed_LM.pdf" and "C:\Working File\Dummy List3) 7-3-07 - 5-30-08.pdf", open if remove the equals sign and parentheses respectively. Ideally, I need to be able to handle any filename that can exist in a windows xp file structure.
 
Marshal
Posts: 79240
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch

I think this question is more difficult than we usually get in beginning Java, so I shall move it.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you try quoting the filenames?
 
Jesse Hayes
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Rob Prime! The Desktop class will be my first choice for opening external files from now on. I did try quoting the filename to answer David Newton. I'm guessing the disparity between characters allowed in DOS filenames and Windows filenames had something to do with the problem.
 
reply
    Bookmark Topic Watch Topic
  • New Topic