2013-06-12 92 views
0

基於this solution我正在訪問pdf文件。代碼如下:爲什麼我的java應用程序無法在USB上找到文件?

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. Ressources
  3. | -----檢驗.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:\ 

這工作完全在我的電腦,並在Eclipse中,但在我的USB記憶棒這是行不通的。出於某種原因,我得到一個IOException。我錯過了什麼?

+2

什麼'IOException'說問題是?請發佈消息和完整的堆棧跟蹤。 – unholysampler

+0

此外,在每種情況下,打印'System.getProperty(「user.dir」)'... – SJuan76

+0

嗨,謝謝你的回覆!添加了堆棧跟蹤和System.getProperty(「user.dir」)的結果。 – Alex

回答

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); 
      } 

     } 
     }); 
相關問題