基于此解决方案,我正在访问 pdf 文件。代码如下:
editor.getMntmNewMenuItem().addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
try {
File pdfFile = new File("Ressources\\test.pdf");
if (pdfFile.exists()) {
if (Desktop.isDesktopSupported()) {
System.out.println(pdfFile.getCanonicalPath());
Desktop.getDesktop().open(pdfFile);
} else {
throw new Exception("Desktop wird nicht unterstützt!");
}
}
else {
throw new Exception("Datei ist nichtdd vorhanden! ");
}
} catch (Exception ex) {
PrintWriter pw = null;
try {
pw = new PrintWriter(new File("stacktrace.txt"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ex.printStackTrace(pw);
pw.append("\n\nUSER DIR: + " +System.getProperty("user.dir"));
pw.close();
JOptionPane.showMessageDialog(editor.getContentPane(), ex.getMessage(), "Fehler",
JOptionPane.ERROR_MESSAGE);
}
}
});
文件结构如下:
- 编辑器.jar
- 资源
- |-----test.pdf
这是完整的堆栈跟踪:
java.io.IOException:无法打开文件:/E://Ressources/test.pdf。错误信息:系统找不到指定的文件。
at sun.awt.windows.WDesktopPeer.ShellExecute(Unknown Source)
at sun.awt.windows.WDesktopPeer.open(Unknown Source)
at java.awt.Desktop.open(Unknown Source)
at iscms.ISCMS$2$20.actionPerformed(ISCMS.java:877)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.AbstractButton.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
USER DIR: = E:\
这在我的电脑和 Eclipse 中完美运行,但在我的 USB 记忆棒上它不起作用。由于某种原因,我得到了一个 IOException。我错过了什么?