Java无法使用运行时打开pdf
我必须在单击 JMenuItem 时打开 pdf。如果我从 netbeans 运行我的程序,我可以通过单击菜单项打开 pdf。但是当我从 jar 文件运行时它没有打开。我清理并构建我的项目。但没有变化。从 netbeans 运行但不从 jar 文件运行时运行。 我需要添加一些库吗?
我的代码如下
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应用程序运行时,我可以从menitem打开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);
}
}
}
});
时,所有 system.out.println() 的输出如下
run:
当从 netbeans 运行第二个代码F:/new/build/classes/newpkg/Documentation.pdf F:\new\build\classes\newpkg\Documentation.pdf 构建成功(总时间:5 秒)
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.
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.
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
run:
F:/new/build/classes/newpkg/Documentation.pdf
F:\new\build\classes\newpkg\Documentation.pdf
BUILD SUCCESSFUL (total time: 5 seconds)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
rundll32.exe
无法处理现在位于 Jar 内的资源。检查getResource(String)
返回的 URL。问题是
rundll32
至少对于 PDF,仅适用于File
实例。使用(例如显示)PDF 的工具通常不设计为接受命令行参数。代表URL
。如果URL
指向File
,则该过程可以继续。但是一旦 PDF 放入 Jar 中,它就只是 Jar 中的一个条目。或者换句话说,它不是文件
。如果该推理正确,则在默认软件中显示 PDF 的一种方法是:
URL
是否指向 Jar(它将包含“!”)。如果是的话..文件
。文件
。rundll32.exe
can not deal with a resource that is now inside a Jar. Inspect the URL returned bygetResource(String)
.The problem is that
rundll32
was, for PDFs at least, only forFile
instances. The tools that consume (e.g. display) PDFs are generally not designed to accept command line args. representing anURL
. If theURL
should turn out to point to aFile
, 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 aFile
.If that reasoning is correct, one way to get the PDF displayed in the default software is to:
URL
to the PDF as done now.URL
points to a Jar (it will contain a '!'). If it does..File
on disk.File
.您可以使用 Java 6 和
Desktop
API?
启动时您可以将文件导出或下载到磁盘吗?
Can you use Java 6 and the
Desktop
API?and on startup can you export or download the file to disk?