为什么我的URI不是分层的?

我有资源文件夹中的文件。 例如,如果我需要从资源文件夹中获取文件,我喜欢这样做:

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

我已经testing过 ,一切正常 ! 一切都很好 ! path是

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

但是 ,如果我创buildjar文件,使用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) 

和文件的path是

文件:/ d:/ JAVA /项目/…/目标/ MyJar.jar /X/Y/Z/myFile.jpg

当我尝试从资源文件夹中获取文件时,会发生此exception。 在这条线上。 为什么? 为什么在JAR文件中有这个问题? 你怎么看?

我正在寻找答案,但他们没有为我工作。

还是有另一种方式,获得资源文件夹path?

你应该使用

 getResourceAsStream(...); 

当资源被捆绑为jar / war或任何其他单个文件包的时候。

看到的事情是,一个jar是一个文件(有点像一个zip文件)一起保存大量的文件。 从Os的pov,它的单个文件,如果你想访问part of the filepart of the file (你的图像文件),你必须使用它作为一个stream。

文档

以下是Eclipse RCP / Plugin开发人员的解决scheme:

 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)非常重要,因为当插件打包到jar中时,这会导致Eclipse在临时位置创build一个未打包的版本,以便可以使用File访问该对象。 例如,我猜Lars Vogel在他的文章中有一个错误 – http://blog.vogella.com/2010/07/06/reading-resources-from-plugin/

当我在我公司工作时,我面临同样的问题。 首先,URI不是hierarichal问题是因为您可能使用“ / ”作为文件分隔符。

你必须记住,“/”是针对Windows,从操作系统到操作系统的变化,在Linux中可能不同。 因此使用File.seperator

所以使用

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

可能会删除不是hierarichal的URI。 但是现在你可能面临一个空指针exception。 我尝试了很多不同的方法,然后使用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文件夹内的目录和子目录。