Cross-Platform File Opening in Java

Discussion in 'Mac Programming' started by russellelly, Mar 2, 2008.

  1. russellelly macrumors regular

    Jun 23, 2006
    Glasgow, UK
    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:

    Code: Failed to open file:/C:/Documents%20and%20Settings/Chris/Desktop/In.Treatment.S01E21.avi. Error message: The parameter is incorrect.
    at Source)
    at Source)
    at 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! :)
  2. bloomberg macrumors newbie

    Sep 8, 2007
    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!
  3. garethlewis2 macrumors 6502

    Dec 6, 2006
    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.
  4. sord macrumors 6502

    Jun 16, 2004
    Java + Windows works just fine with / instead of \ for file separators, I use them all the time at work...

Share This Page