aspose file tools
The moose likes Java in General and the fly likes Using Runtime to open a file with parentheses in the name Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Using Runtime to open a file with parentheses in the name" Watch "Using Runtime to open a file with parentheses in the name" New topic
Author

Using Runtime to open a file with parentheses in the name

Jesse Hayes
Greenhorn

Joined: Jul 30, 2009
Posts: 3
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?)
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16479
    
    2

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.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

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).


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Jesse Hayes
Greenhorn

Joined: Jul 30, 2009
Posts: 3
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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32599
    
    4
Welcome to JavaRanch

I think this question is more difficult than we usually get in beginning Java, so I shall move it.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Did you try quoting the filenames?
Jesse Hayes
Greenhorn

Joined: Jul 30, 2009
Posts: 3
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using Runtime to open a file with parentheses in the name
 
Similar Threads
A project someone gave me...Renaming files
Wireshark connectivity using java and string compare from text file
File I/O
Save a pdf or any file to client machine from server
Space within a string?