Using Runtime to open a file with parentheses in the name
Jesse Hayes
Greenhorn
Joined: Jul 30, 2009
Posts: 3
posted
0
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?)
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.
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
posted
0
Welcome to JavaRanch
I think this question is more difficult than we usually get in beginning Java, so I shall move it.
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.