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

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. >:(

解决方案

I never knew about this Desktop command, untill recently through this post:

would Java's Runtime.getRuntime().exec() run on 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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值