java classloader.getSystemResourceAsStream 返回 null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/770107/
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-10-29 13:41:29  来源:igfitidea点击:

classloader.getSystemResourceAsStream returns null

javapropertiesclassloader

提问by Lancelot

I'm trying to load a properties file without using the actual path of the file. I've already done that on some other simple apps using:

我试图在不使用文件的实际路径的情况下加载属性文件。我已经在其他一些简单的应用程序上使用过:

InputStream inputStream = ClassLoader.getSystemResourceAsStream(PROPERTIES_FILE);
props.load(inputStream);

But this time it doesn't work. The inputStream is null for some reason. PROPERTIES_FILE is a constant defined as "app.properties". I tried to remove the .properties extension and got the same results.

但这一次不起作用。由于某种原因, inputStream 为空。PROPERTIES_FILE 是定义为“app.properties”的常量。我试图删除 .properties 扩展名并得到相同的结果。

Any ideas?

有任何想法吗?

Thanks.

谢谢。

回答by jconlin

The PROPERTIES_FILE constant should include the package as well as the properties file (e.g. "com/some/library/file.properties".

PROPERTIES_FILE 常量应该包括包以及属性文件(例如“com/some/library/file.properties”。

    final static String PROPS_FILE = "/com/some/library/file.props";
                     //The preceding  "/" is dependendant on wheterh 
                     //you are going to be giving a relative or absolute location
    InputStream is = YourCurrentClass.class.getResourceAsStream(PROPS_FILE);

回答by erickson

When getSystemResourceAsStreamreturns null, it means the resource was not found. Make sure the requested resource really is on the classpath at the specified location.

getSystemResourceAsStream返回时null,表示没有找到资源。确保请求的资源确实位于指定位置的类路径上。

回答by Gogogo

Got the same problem.

遇到了同样的问题。

Reason: I renamed DAOpackage to dao. While exploding the artifact, directory DAOnot get overwritten.

原因:我将DAO包重命名为dao. 爆炸时artifact,目录DAO不会被覆盖。

So I got daoin project internals and DAOin filesystem :facepalm:

所以我进入dao了项目内部和DAO文件系统:facepalm: