PDA

View Full Version : Cross-Platform File Opening in Java




russellelly
Mar 2, 2008, 10:02 AM
Hi all,

Just spent some hours banging my head on a brick wall with this one. I've got a program where I have to open a file given its pathname as a String. I've got code that works fine on Mac and Linux, but it'll only work on Windows XP if the pathname for certain file types - for example .m4a will open with iTunes quite happily, but .avi produces this error:

java.io.IOException: Failed to open file:/C:/Documents%20and%20Settings/Chris/Desktop/In.Treatment.S01E21.avi. Error message: The parameter is incorrect.

at sun.awt.windows.WDesktopPeer.ShellExecute(Unknown Source)
at sun.awt.windows.WDesktopPeer.open(Unknown Source)
at java.awt.Desktop.open(Unknown Source)

(the stack trace goes on a while, but I won't type it all out unless more is needed)

I've tried this with the default application for .avi being Windows Media Player and VLC (that error is with the latter set, the former gives a similar but slightly different error). Does Windows XP just not fully support the java.awt.Desktop class?

Any ideas very welcome! :)



sord
Mar 2, 2008, 08:44 PM
Does it look like this? http://bugs.sun.com/view_bug.do?bug_id=6599987

bloomberg
Mar 3, 2008, 02:21 AM
The first problem is that the file path separator should be a backslash on windows, not a slash. (also remember you need two of them within a string.) It's good practice to use a regular expression and replace path elements with File.separator which is cross-platform.

The second issue, but am not sure about this, is the file:/ at the beginning of your path. Not sure if this is your debugging statement or part of the path but I don't think you need it... but if you do use it, it should be file:/// on windows.

Edit: Only follow one of the two suggestions above but not both. I.e. remove the proceeding file: and use backslashes OR change it to file:///. Good luck!

garethlewis2
Mar 3, 2008, 02:51 AM
If you're using Unix style path seperartors that will be your problem. Rather than farting about trying to write code the determines what is running on, just use the System property "pathseperator" in Java and use this to construct the string where you want to load the file from. If that doesn't work your code is incorrect. We use that style here.

sord
Mar 3, 2008, 04:18 PM
Java + Windows works just fine with / instead of \ for file separators, I use them all the time at work...