tags:

views:

868

answers:

1

It looks like I cannot use Desktop.open() on PDF files regardless of location. Here's a small test program:

package com.example.bugs;

import java.awt.Desktop;
import java.io.File;
import java.io.IOException;

public class DesktopOpenBug {
    static public void main(String[] args)
    {
        try {
         Desktop desktop = null;
            // Before more Desktop API is used, first check 
            // whether the API is supported by this particular 
            // virtual machine (VM) on this particular host.
            if (Desktop.isDesktopSupported()) {
                desktop = Desktop.getDesktop();
                for (String path : args)
                {
                 File file = new File(path);
                 System.out.println("Opening "+file);
                 desktop.open(file);
                }
            }         
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

If I run DesktopOpenBug with arguments c:\tmp\zz1.txt c:\tmp\zz.xml c:\tmp\ss.pdf (3 files I happen to have lying around) I get this result: (the .txt and .xml files open up fine)

Opening c:\tmp\zz1.txt
Opening c:\tmp\zz.xml
Opening c:\tmp\ss.pdf
java.io.IOException: Failed to open file:/c:/tmp/ss.pdf. 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)
at com.example.bugs.DesktopOpenBug.main(DesktopOpenBug.java:21)

What the heck is going on? I'm running WinXP, I can type "c:\tmp\ss.pdf" at the command prompt and it opens up just fine.

edit: if this is an example of Sun Java bug #6764271 please help by voting for it. What a pain. >:(

+1  A: 

If you switch the order of your arugments does that cause one of the other files to get that same error. I wonder if you need to trim the end of the path before calling the File constructor.

umm...yeah ignore that... check this link java.sun.com/javase/6/…) ...open throws a IO exception "if the specified file has no associated application or the associated application fails to be launched " ... also from the top of the page... "The mechanism of registereing, accessing, and launching the associated application is platform-dependent. "


I am a new user so I can only post one URL...bs.

code for the Desktop class: (h t t p : / / fuseyism.com/classpath/doc/java/awt/Desktop-source.html)

the open method calls DesktopPeer.open

DesktopPeer source: (h t t p : / / www.jdocs.com/javase/7.b12/java/awt/peer/DesktopPeer.html)

DesktopPeer is implementation specific.

ok...here is source for a windows specific implementation: (h t t p : / / www.java2s.com/Open-Source/Java-Document/6.0-JDK-Platform/windows/sun/awt/windows/WDesktopPeer.java.htm)

open->ShellExecute->(Native)ShellExecute..

Native ShellExecute is a wrapper for Win32 ShellExecute. Here is info on the function. (h t t p : / / msdn.microsoft.com/en-us/library/bb762153(VS.85).aspx)

My suggestion for a work around would be to write your own implmentation of the ShellExecute function. Here is source from someone who did it. (h t t p : / / www.heimetli.ch/shellexec.html)

Good Luck!

Jon
umm...yeah ignore that... check this link http://java.sun.com/javase/6/docs/api/java/awt/Desktop.html#open(java.io.File) ...open throws a IO exception "if the specified file has no associated application or the associated application fails to be launched " ... also from the top of the page... "The mechanism of registereing, accessing, and launching the associated application is platform-dependent. "
Jon
can you just edit your answer?
Jason S
PDF files have an associated application on my machine and I have no idea how to debug what is going on. All it says is "The parameter is incorrect" which sounds like a Microsoft COM/ActiveX bug.
Jason S