为什么我的URI没有分层?

时间:2013-08-05 09:51:11

标签: java maven jar java-7 executable-jar

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

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

我已经测试,所有都可以

路径是

/ d:/ JAVA /项目/.../类/ 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文件中存在这个问题?你觉得怎么样?

还有另一种方法,获取资源文件夹路径吗?

3 个答案:

答案 0 :(得分:68)

您应该使用

getResourceAsStream(...);

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

看到的是,jar是一个单独的文件(有点像zip文件),它们将大量文件放在一起。从Os的pov,它是一个单独的文件,如果你想访问part of the file(你的图像文件),你必须将它用作流。

Documentation

答案 1 :(得分:11)

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

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在临时位置创建一个解压缩版本,以便可以使用File访问该对象。例如,我猜Lars Vogel在他的文章中有一个错误 - http://blog.vogella.com/2010/07/06/reading-resources-from-plugin/

答案 2 :(得分:1)

当我在公司工作时,我面临同样的问题。首先,URI不是hierarichal问题是因为你可能正在使用&#34; / &#34;作为文件分隔符。

你必须记住&#34; /&#34;适用于Windows,从操作系统到操作系统更改,在Linux中可能有所不同。因此,请使用 File.seperator

所以使用

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

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

相关问题