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. editor.jar
  2. リソース
  3. |-----test.pdf

これは完全な stackTrace です。

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:\

これは、私の PC と 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 に答える