0

基于此解决方案,我正在访问 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);
            }

        }
        });

文件结构如下:

  1. 编辑器.jar
  2. 资源
  3. |-----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。我错过了什么?

4

1 回答 1

0

下面的解决方案确实有效。我不再使用相对路径,而是使用System.get.property("user.dir"). 我不知道这是否又好又干净,但是如果这有效,为什么要抱怨呢?它没有回答问题,但它确实解决了我的问题。

        editor.getMntmNewMenuItem().addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            try {
            String userDir=System.getProperty("user.dir");
            File pdfFile = new File(userDir+"\\Ressources\\test.pdf");
            if (pdfFile.exists()) {

                if (Desktop.isDesktopSupported()) {
                Desktop.getDesktop().open(pdfFile);
                } else {
                throw new Exception("Desktop wird nicht unterstützt!");
                }
            } 
            else {
                throw new Exception("Datei ist nicht vorhanden! ");
            }
            } catch (Exception ex) {
JOptionPane.showMessageDialog(editor.getContentPane(), ex.getMessage(), "Fehler",
                JOptionPane.ERROR_MESSAGE);
            }

        }
        });
于 2013-06-12T20:25:49.663 回答