eclipse 在不同的包中类 getResourceAsStream() 资源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16320170/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 20:26:42  来源:igfitidea点击:

Class getResourceAsStream() resource in a different package

javaeclipseclassloader

提问by Sotirios Delimanolis

I have something like the following project structure

我有类似以下项目结构的东西

-project 
    -src/main/java // source folder
        -com.mypackage
            -Utility.java
            -some.properties
    -src/main/resources // source folder
        -configuration_files
            -configuration_report.txt

I'm generating the jarwith Eclipse's Exportfunction. The above translates to a jarwith the structure:

我正在jar用 Eclipse 的Export函数生成。以上转换为jar具有结构的a:

-myjar.jar
    -com
        -mypackage
            -Utility.class
            -some.properties
    -configuration_files
        -configuration_report.txt

Within the Utilityclass, I'm trying to get the InputStreamfrom configuration_report.txt.

Utility课堂上,我试图InputStreamconfiguration_report.txt.

I was doing this (it's within a staticmethod):

我正在这样做(它在一个static方法中):

Utility.class.getResourceAsStream("/configuration_files/configuration_report.txt"); 

which seems to be validated by the answer to this question, since the file is not in the same package as the class requesting it.

这似乎得到了这个问题的答案的验证,因为该文件与请求它的类不在同一个包中。

When I run it, however, it returns null. Why?

但是,当我运行它时,它返回null. 为什么?

Note that for the some.propertiesfile, I could do both

请注意,对于some.properties文件,我可以同时执行

Utility.class.getResourceAsStream("/com/package/some.properties");
Utility.class.getResourceAsStream("some.properties");

and I would get the stream.

我会得到流。

I'm running this from the command line if it makes any difference.

如果它有任何不同,我将从命令行运行它。

java [-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=1044] -cp myjar.jar MyMain

If I try to run it within Eclipse, it works...

如果我尝试在 Eclipse 中运行它,它会起作用......

Edit:

编辑:

This code is relevant. With or without the preceding /, it return null

此代码是相关的。有或没有前面的/,它返回null

    String sourceFilePath = "/configuration_files" + File.separator + "configuration_report.txt";
    InputStream in = Utility.class.getResourceAsStream(sourceFilePath);

回答by Sotirios Delimanolis

After many tries, the solution comes down to this little javadoc entry for ClassLoader#getResource(String):

经过多次尝试,解决方案归结为以下 javadoc 小条目ClassLoader#getResource(String)

The name of a resource is a '/'-separated path name that identifies the resource.

资源的名称是一个以“/”分隔的路径名,用于标识资源。

The snippet I had originally posted would've worked

我最初发布的片段会起作用

Utility.class.getResourceAsStream("/configuration_files/configuration_report.txt"); 

but I wasn't using /, I was using File.separatorlike in my edit:

但我没有使用/,我File.separator在编辑中使用了like:

String sourceFilePath = "/configuration_files" + File.separator + "configuration_report.txt";

thinking the class loader worked like OS specific File operations. The first /was matched, but the File.separatorevaluated to \\since I'm on Windows.

认为类加载器像操作系统特定的文件操作一样工作。第一个/匹配,但由于我在 Windows 上被File.separator评估为\\

The javadoc for Class#getResource(String)says more of the same.

javadoc forClass#getResource(String)说明了更多相同的内容。

Where the modified_package_name is the package name of this object with '/' substituted for '.' ('\u002e').

其中 modified_pa​​ckage_name 是这个对象的包名,用“/”代替“.” ('\u002e')。

As to why this worked on Eclipse, but not from the command line, I'm still unsure, but look at @andy's comment below.

至于为什么这在 Eclipse 上起作用,而不是从命令行起作用,我仍然不确定,但请看下面@andy 的评论。

This articlegives is a good explanation of this situation.

这篇文章对这种情况给出了很好的解释。