2013-08-05 25 views
38

我在資源文件夾中有文件。例如,如果我需要從資源文件夾中獲取文件,我做這樣的:爲什麼我的URI不分層?

File myFile= new File(MyClass.class.getResource(/myFile.jpg).toURI());    
System.out.println(MyClass.class.getResource(/myFile.jpg).getPath()); 

測試工作一切!

的路徑是

/D:/java/projects/.../classes/X/Y/Z/myFile.jpg

,如果我創建jar文件,使用, Maven的

mvn package 

...,然後開始我的應用程序:

java -jar MyJar.jar 

我有以下錯誤:

Exception in thread "Thread-4" java.lang.RuntimeException: ხელმოწერის განხორციელება შეუძლებელია 
Caused by: java.lang.IllegalArgumentException: URI is not hierarchical 
     at java.io.File.<init>(File.java:363) 

... 和文件的路徑爲

file:/D:/java/projects/.../target/MyJar.jar!/X/Y/Z/myFile.jpg 

此異常時,我嘗試從資源文件夾文件發生。在這條線上。爲什麼?爲什麼在JAR文件中存在這個問題?你怎麼看?

是否有另一種方法獲取資源文件夾路徑?

+4

你可以看看這個:http://stackoverflow.com/questions/10144210/java-jar-file-use-resource-errors-uri-is-not-hierarchical和接受的答案。 – cyroxx

回答

57

您應該使用

getResourceAsStream(...); 

當資源被打包成JAR /戰爭或其他任何一個文件包爲這一問題。

看到的東西是,一個jar文件是一個文件(有點像zip文件),它們將大量文件放在一起。從Os的pov,它的單個文件,如果你想訪問part of the file(你的圖像文件),你必須使用它作爲一個流。

Documentation

10

這裏是Eclipse RCP /插件開發人員提供瞭解決方案:

Bundle bundle = Platform.getBundle("resource_from_some_plugin"); 
URL fileURL = bundle.getEntry("files/test.txt"); 
File file = null; 
try { 
    URL resolvedFileURL = FileLocator.toFileURL(fileURL); 

    // We need to use the 3-arg constructor of URI in order to properly escape file system chars 
    URI resolvedURI = new URI(resolvedFileURL.getProtocol(), resolvedFileURL.getPath(), null); 
    File file = new File(resolvedURI); 
} catch (URISyntaxException e1) { 
    e1.printStackTrace(); 
} catch (IOException e1) { 
    e1.printStackTrace(); 
} 

使用FileLocator.toFileURL(fileURL)而非resolve(fileURL) ,因爲它是非常重要的,當這個插件被打包到一個罐子裏,這將導致Eclipse在臨時位置創建一個解壓縮版本,以便可以使用File訪問該對象。例如,我猜Lars Vogel在他的文章中有一個錯誤 - http://blog.vogella.com/2010/07/06/reading-resources-from-plugin/

+3

謝謝,我欠你一杯啤酒,確實Vogella的文章需要更新。 –

+0

@Vladllie愛它,它幫助你;-) –

+0

http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Freference%2Fapi%2Forg%2Feclipse%2Fcore %2Fruntime%2FFileLocator.html 此解決方案似乎與平臺相關。 有沒有辦法實現類似的IntelliJ而不必導入「org.eclipse.core.runtime.FileLocator」? – Kwoinkwoin

0

當我在我公司的一個項目中工作時,我面臨同樣的問題。首先,URI不是hierarichal問題是因爲您可能使用「/」作爲文件分隔符。

您必須記住,「/」是針對Windows的,從操作系統到操作系統它會發生變化,它可能在Linux中不同。因此使用File.seperator

因此,使用

this.getClass().getClassLoader().getResource("res"+File.seperator+"secondFolder") 

可以刪除URI不hierarichal。但是現在您可能會遇到空指針異常。我嘗試了許多不同的方法,然後使用JarEntries類來解決它。

File jarFile = new File(this.getClass().getProtectionDomain().getCodeSource().getLocation().toURI().getPath()); 
     String actualFile = jarFile.getParentFile().getAbsolutePath()+File.separator+"Name_Of_Jar_File.jar"; 
     System.out.println("jarFile is : "+jarFile.getAbsolutePath()); 
     System.out.println("actulaFilePath is : "+actualFile); 
     final JarFile jar = new JarFile(actualFile); 
     final Enumeration<JarEntry> entries = jar.entries(); //gives ALL entries in jar 
     System.out.println("Reading entries in jar file "); 
     while(entries.hasMoreElements()) { 
      JarEntry jarEntry = entries.nextElement(); 
      final String name = jarEntry.getName(); 
      if (name.startsWith("Might Specify a folder name you are searching for")) { //filter according to the path 
       System.out.println("file name is "+name); 
       System.out.println("is directory : "+jarEntry.isDirectory()); 
       File scriptsFile = new File(name); 
       System.out.println("file names are : "+scriptsFile.getAbsolutePath()); 

      } 
     } 
     jar.close(); 

您必須在此明確指定jar名稱。所以使用這個代碼,這會給你在jar文件夾內的目錄和子目錄。

相關問題