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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1527162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 16:55:21  来源:igfitidea点击:

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

javadesktop

提问by Jason S

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

看起来无论位置如何,我都无法在 PDF 文件上使用 Desktop.open()。这是一个小测试程序:

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)

如果我运行带有参数的 DesktopOpenBug 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 #6764271please help by voting for it. What a pain. >:(

编辑:如果这是Sun Java 错误 #6764271的示例,请投票支持。多么痛苦。>:(

回答by David

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.

它一直对我有用。如果你的方法不起作用,可能你可以考虑试试这个命令。

回答by Jon

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.

如果您切换参数的顺序,是否会导致其他文件之一出现相同的错误。我想知道在调用 File 构造函数之前是否需要修剪路径的末尾。

umm...yeah ignore that... check the documentation of Desktop.open. openthrows 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. "

嗯...是的忽略那个...检查文档Desktop.openopen抛出一个IO异常“如果指定的文件没有关联的应用程序或者关联的应用程序启动失败”...也是从页面顶部...“注册、访问和启动关联应用程序的机制是平台——依赖。”



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

桌面类的代码: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

open->ShellExecute->(Native)ShellExecute

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

NativeShellExecute是 Win32 的包装器ShellExecute。这是有关该功能的信息。 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. Here is source from someone who did it. http://www.heimetli.ch/shellexec.html

我的解决建议是编写您自己的 ShellExecute 函数实现。这是来自做过这件事的人的来源。 http://www.heimetli.ch/shellexec.html