[英]Java unable to open pdf using Runtime
I have to open a pdf on clicking a JMenuItem.我必须在单击 JMenuItem 时打开 pdf。 I can open the pdf on click the menu item if i run my program from netbeans.
如果我从 netbeans 运行我的程序,我可以在单击菜单项时打开 pdf。 But when i run from jar file it is not opening.
但是当我从 jar 文件运行时,它没有打开。 I clean and build my project.
我清理并构建我的项目。 But no change.
但没有变化。 Running when run from netbeans but not running from jar file.
从 netbeans 运行但不从 jar 文件运行时运行。 Do i need to add some library.
我需要添加一些库吗?
My codes are as follows我的代码如下
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);
}
}
});
Tried this as well but getting same thing.. i can open pdf from menuitem when i run from netbeans but not from jar application.也试过这个但得到同样的东西..当我从 netbeans 但不能从 jar 应用程序运行时,我可以从 menuitem 打开 pdf。
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);
}
}
}
});
The output for all the system.out.println() is as follows when run from netbeans for this second code对于第二个代码,从 netbeans 运行时,所有 system.out.println() 的 output 如下所示
run:
F:/new/build/classes/newpkg/Documentation.pdf F:\new\build\classes\newpkg\Documentation.pdf BUILD SUCCESSFUL (total time: 5 seconds) F:/new/build/classes/newpkg/Documentation.pdf F:\new\build\classes\newpkg\Documentation.pdf 构建成功(总时间:5 秒)
rundll32.exe
can not deal with a resource that is now inside a Jar. rundll32.exe
无法处理现在位于 Jar 内的资源。 Inspect the URL returned by getResource(String)
.检查
getResource(String)
返回的 URL 。
..but still not working..
..但仍然无法正常工作..
The problem is that rundll32
was, for PDFs at least, only for File
instances.问题在于,至少对于 PDF,
rundll32
仅适用于File
实例。 The tools that consume (eg display) PDFs are generally not designed to accept command line args.使用(例如显示)PDF 的工具通常不是为接受命令行参数而设计的。 representing an
URL
.代表
URL
。 If the URL
should turn out to point to a File
, the process can proceed.如果
URL
应该指向File
,则该过程可以继续。 But once the PDF is in a Jar, it is just an entry in a Jar.但是一旦 PDF 在 Jar 中,它就只是 Jar 中的一个条目。 Or to put that another way, it is not a
File
.或者换句话说,它不是
File
。
If that reasoning is correct, one way to get the PDF displayed in the default software is to:如果该推理是正确的,在默认软件中显示 PDF 的一种方法是:
URL
to the PDF as done now.URL
。URL
points to a Jar (it will contain a '.').URL
指向 Jar(它将包含“.”)。 If it does..File
on disk.File
。File
.File
。 try //try statement
{
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + "c:\\chart.pdf"); //open the file chart.pdf
} catch (Exception e) //catch any exceptions here
{
System.out.println("Error" + e ); //print the error
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.