java.awt.Desktop.open不能用于PDF文件? [英] java.awt.Desktop.open doesn’t work with PDF files?

查看:143
本文介绍了java.awt.Desktop.open不能用于PDF文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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();
        }
    }
}

如果我使用参数运行DesktopOpenBug c:\ tmp\zz1.txt c:\ tmp\zz.xml c:\ tmp \\\。pdf (我碰巧有3个档案我得到这个结果:( .txt和.xml文件打开正常)

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)

到底发生了什么事?我正在运行WinXP,我可以在命令提示符下输入c:\ tmp \\\。pdf并打开就好了。

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.

编辑:如果这是 Sun Java bug#6764271 请投票帮助。多么痛苦。 > :(

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

推荐答案

我从来不知道这个桌面命令,直到最近通过这篇文章:

<如果Java的Runtime.getRuntime()。exec()在Windows 7上运行,那么这将是一个很好的例子。hps =https://tackoverflow.com/questions/4571604/would-javas-runtime-getruntime-exec-run-on-windows-7> ?

I never knew about this Desktop command, untill recently through this post:
would Java's Runtime.getRuntime().exec() run on windows 7?

以前我一直在使用:

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.

这篇关于java.awt.Desktop.open不能用于PDF文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆