Java无法使用运行时打开pdf [英] Java unable to open pdf using Runtime
问题描述
我必须在单击JMenuItem时打开pdf.如果我从netbeans运行程序,则可以单击菜单项打开pdf.但是,当我从jar文件运行时,它无法打开.我清理并构建我的项目.但是没有变化.从netbeans运行时运行,但不是从jar文件运行时运行. 我需要添加一些库吗?
I have to open a pdf on clicking a JMenuItem. I can open the pdf on click the menu item if i run my program from netbeans. But when i run from jar file it is not opening. I clean and build my project. But no change. Running when run from netbeans but not running from jar file. Do i need to add some library.
我的代码如下
m_aboutItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Runtime rt = Runtime.getRuntime();
//System.out.println(Menubar1.getDefaultLocale());
URL link2=Menubar1.class.getResource("/newpkg/Documentation.pdf");
String link=link2.toString();
link=link.substring(6);
System.out.println(link);
System.out.println(link2);
String link3="F:/new/build/classes/newpkg/Documentation.pdf";
try {
Process proc = rt.exec("rundll32.exe url.dll,FileProtocolHandler " + link2);
} catch (IOException ex) {
Logger.getLogger(Menubar1.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
也尝试过此操作,但得到了同样的结果..当我从netbeans运行但不能从jar应用程序运行时,可以从menuitem打开pdf.
Tried this as well but getting same thing.. i can open pdf from menuitem when i run from netbeans but not from jar application.
m_aboutItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (Desktop.isDesktopSupported()) {
Desktop desktop = Desktop.getDesktop();
URL link2=Menubar1.class.getResource("/newpkg/Documentation.pdf");
String link=link2.toString();
link=link.substring(6);
System.out.println(link);
File file=new File(link);
System.out.println(file);
try {
desktop.open(file);
} catch (IOException ex) {
Logger.getLogger(Menubar1.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
});
对于第二个代码,从netbeans运行时,所有system.out.println()的输出如下所示:
The output for all the system.out.println() is as follows when run from netbeans for this second code
run:
F:/new/build/classes/newpkg/Documentation.pdf F:\ new \ build \ classes \ newpkg \ Documentation.pdf 建立成功(总时间:5秒)
F:/new/build/classes/newpkg/Documentation.pdf F:\new\build\classes\newpkg\Documentation.pdf BUILD SUCCESSFUL (total time: 5 seconds)
推荐答案
rundll32.exe
无法处理Jar中现在存在的资源.检查getResource(String)
返回的URL.
rundll32.exe
can not deal with a resource that is now inside a Jar. Inspect the URL returned by getResource(String)
.
..但仍然无法正常工作..
..but still not working..
问题在于,至少对于PDF,rundll32
仅用于File
实例.使用(例如显示)PDF的工具通常不设计为接受命令行参数.代表URL
.如果URL
应该指向File
,则该过程可以继续.但是,一旦将PDF放入Jar中,它就只是Jar中的一个条目.或者换一种说法,它是不是File
.
The problem is that rundll32
was, for PDFs at least, only for File
instances. The tools that consume (e.g. display) PDFs are generally not designed to accept command line args. representing an URL
. If the URL
should turn out to point to a File
, the process can proceed. But once the PDF is in a Jar, it is just an entry in a Jar. Or to put that another way, it is not a File
.
如果该推理是正确的,则在默认软件中显示PDF的一种方法是:
If that reasoning is correct, one way to get the PDF displayed in the default software is to:
- 立即将
URL
获取到PDF. - 检查
URL
是否指向Jar(它将包含'!').如果是的话..
- Get the
URL
to the PDF as done now. - Check if the
URL
points to a Jar (it will contain a '!'). If it does..
- 从Jar中提取PDF到磁盘上的(临时)
File
.
File
.
File
.这篇关于Java无法使用运行时打开pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!