简体   繁体   English

java.awt.Desktop.open不能用于PDF文件?

[英]java.awt.Desktop.open doesn’t work with PDF files?

It looks like I cannot use Desktop.open() on PDF files regardless of location. 看起来我不能在PDF文件上使用Desktop.open()而不管位置如何。 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) 如果我运行带有参数c:\\tmp\\zz1.txt c:\\tmp\\zz.xml c:\\tmp\\ss.pdf (我碰巧有3个文件)我得到了这个结果:( .txt和.xml文件打开很好)

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. 我正在运行WinXP,我可以在命令提示符下键入“c:\\ tmp \\ ss.pdf”,它打开就好了。

edit: if this is an example of Sun Java bug #6764271 please help by voting for it. 编辑:如果这是Sun Java bug#6764271的一个例子,请通过投票帮助它。 What a pain. 太痛苦了。 >:( > :(

I never knew about this Desktop command, untill recently through this post: 我从来不知道这个桌面命令,直到最近通过这篇文章:
would Java's Runtime.getRuntime().exec() run on windows 7? Java的Runtime.getRuntime()。exec()会在Windows 7上运行吗?

Previously i have been using: 以前我一直在使用:

Runtime.getRuntime().exec("rundll32 SHELL32.DLL,ShellExec_RunDLL "+ myfile); 

And it has always worked for me. 它一直对我有用。 If your method does not work, may be you can think about try this command. 如果您的方法不起作用,可能您可以考虑尝试此命令。

If you switch the order of your arugments does that cause one of the other files to get that same error. 如果你切换你的arugments的顺序,那会导致其他一个文件得到同样的错误。 I wonder if you need to trim the end of the path before calling the File constructor. 我想知道在调用File构造函数之前是否需要修剪路径的末尾。

umm...yeah ignore that... check the documentation of Desktop.open . 嗯...是的,请忽略它...查看Desktop.open的文档 open throws an 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. " open抛出IO异常“如果指定的文件没有关联的应用程序或关联的应用程序无法启动”...也从页面顶部...“注册,访问和启动相关应用程序的机制是平台依赖。“


code for the Desktop class: http://fuseyism.com/classpath/doc/java/awt/Desktop-source.html Desktop类的代码: http//fuseyism.com/classpath/doc/java/awt/Desktop-source.html

The open method calls DesktopPeer.open . open方法调用DesktopPeer.open

DesktopPeer source: http://www.jdocs.com/javase/7.b12/java/awt/peer/DesktopPeer.html DesktopPeer来源: http//www.jdocs.com/javase/7.b12/java/awt/peer/DesktopPeer.html

DesktopPeer is implementation specific. DesktopPeer是特定于实现的。

Here is source for a Windows-specific implementation: http://www.java2s.com/Open-Source/Java-Document/6.0-JDK-Platform/windows/sun/awt/windows/WDesktopPeer.java.htm 以下是Windows特定实现的来源: http//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 . Native ShellExecute是Win32 ShellExecute的包装器。 Here is info on the function. 这是关于功能的信息。 http://msdn.microsoft.com/en-us/library/bb762153(VS.85).aspx http://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. 我对解决方法的建议是编写自己的ShellExecute函数实现。 Here is source from someone who did it. 这是来自某人的来源。 http://www.heimetli.ch/shellexec.html http://www.heimetli.ch/shellexec.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM