I have a project structure like this:
我有这样的项目结构:
src |-main |-java |-com.abc.xyz |-Login.java
I have to add a resource file to this and read the resource with
我必须向此添加资源文件并使用以下方式读取资源
nputStream is = getClass().getResourceAsStream("launchers.properties");
This is giving null.
这是空的。
In intellij I am not able to add a new package under src/main for resources
folder sothat the project structure looks like this. How can I the launchers.properties
resource file into the project?
在intellij中,我无法在src / main下为资源文件夹添加一个新的包,项目结构如下所示。我怎样才能将launchers.properties资源文件导入到项目中?
src |-main |-java |-com.abc.xyz |-Login.java |-resources |-com.abc.xyz |-Login |-launcher.properties
I tried the solution suggested by @maba but still not working
我尝试了@maba建议的解决方案,但仍然无法正常工作
2 个解决方案
#1
52
The launcher.properties
should not be under a folder called Login
. It should be placed directly in the src/main/resources/com/abc/xyz
folder.
launcher.properties不应位于名为Login的文件夹下。它应该直接放在src / main / resources / com / abc / xyz文件夹中。
It is really as simple as I said but if the resources folder is not marked as a sources folder then this may be the problem.
这真的很简单,但如果资源文件夹没有标记为源文件夹,那么这可能就是问题所在。
This is the initial class and setup:
这是初始类和设置:
Now create the resources folder:
现在创建资源文件夹:
This newly created folder should be automatically marked as a sources folder and if it is blue color marked then it is. Otherwise you'll have to mark it manually:
这个新创建的文件夹应该自动标记为源文件夹,如果是蓝色标记,那么它就是。否则你必须手动标记它:
Now you'll be able to add packages to it:
现在,您将能够向其添加包:
And now you can add the file to it:
现在您可以将文件添加到它:
And rerunning the application will not give you any null
value back:
并且重新运行应用程序将不会返回任何null值:
And the package view will surely show the launchers.properties
file as well:
包视图肯定会显示launchers.properties文件:
#2
3
As @maba pointed out, your properties file should be in the same package as your class for your code to work.
正如@maba指出的那样,您的属性文件应该与您的类在同一个包中,以便您的代码可以工作。
So, you should have two files:
所以,你应该有两个文件:
- src/main/java/com/abc/xyz/Login.java
- src/main/resources/com/abc/xyz/launcher.properties
If IntelliJ is showing the resource or not is beside the question. What you need to do is check if the results are included in your target artefact.
如果IntelliJ显示资源与否,则问题旁边。您需要做的是检查结果是否包含在目标人工制品中。
Do a build all
in IntelliJ, open up the resulting WAR/JAR/EAR with your favorite ZIP viewer and browse into the "com/abc/xyz" folder. You should see both files there.
在IntelliJ中进行构建,使用您喜欢的ZIP查看器打开生成的WAR / JAR / EAR,然后浏览到“com / abc / xyz”文件夹。你应该在那里看到两个文件。
- If they are, you are doing something wrong in your code. Check for typos, especially dots and spaces at the end or beginning (e.g. "launcher.properties[space]"), copy/paste the file name to make sure
- If they are not there, your IntelliJ setup is wrong. Resources do not get included in your target build. Check online for tutorials how to do this with IntelliJ idea.
如果是,您在代码中做错了。检查拼写错误,特别是结尾或开头的点和空格(例如“launcher.properties [space]”),复制/粘贴文件名以确保
如果它们不存在,则IntelliJ设置错误。资源不会包含在您的目标构建中。使用IntelliJ创意在线查看如何执行此操作的教程。
#1
52
The launcher.properties
should not be under a folder called Login
. It should be placed directly in the src/main/resources/com/abc/xyz
folder.
launcher.properties不应位于名为Login的文件夹下。它应该直接放在src / main / resources / com / abc / xyz文件夹中。
It is really as simple as I said but if the resources folder is not marked as a sources folder then this may be the problem.
这真的很简单,但如果资源文件夹没有标记为源文件夹,那么这可能就是问题所在。
This is the initial class and setup:
这是初始类和设置:
Now create the resources folder:
现在创建资源文件夹:
This newly created folder should be automatically marked as a sources folder and if it is blue color marked then it is. Otherwise you'll have to mark it manually:
这个新创建的文件夹应该自动标记为源文件夹,如果是蓝色标记,那么它就是。否则你必须手动标记它:
Now you'll be able to add packages to it:
现在,您将能够向其添加包:
And now you can add the file to it:
现在您可以将文件添加到它:
And rerunning the application will not give you any null
value back:
并且重新运行应用程序将不会返回任何null值:
And the package view will surely show the launchers.properties
file as well:
包视图肯定会显示launchers.properties文件:
#2
3
As @maba pointed out, your properties file should be in the same package as your class for your code to work.
正如@maba指出的那样,您的属性文件应该与您的类在同一个包中,以便您的代码可以工作。
So, you should have two files:
所以,你应该有两个文件:
- src/main/java/com/abc/xyz/Login.java
- src/main/resources/com/abc/xyz/launcher.properties
If IntelliJ is showing the resource or not is beside the question. What you need to do is check if the results are included in your target artefact.
如果IntelliJ显示资源与否,则问题旁边。您需要做的是检查结果是否包含在目标人工制品中。
Do a build all
in IntelliJ, open up the resulting WAR/JAR/EAR with your favorite ZIP viewer and browse into the "com/abc/xyz" folder. You should see both files there.
在IntelliJ中进行构建,使用您喜欢的ZIP查看器打开生成的WAR / JAR / EAR,然后浏览到“com / abc / xyz”文件夹。你应该在那里看到两个文件。
- If they are, you are doing something wrong in your code. Check for typos, especially dots and spaces at the end or beginning (e.g. "launcher.properties[space]"), copy/paste the file name to make sure
- If they are not there, your IntelliJ setup is wrong. Resources do not get included in your target build. Check online for tutorials how to do this with IntelliJ idea.
如果是,您在代码中做错了。检查拼写错误,特别是结尾或开头的点和空格(例如“launcher.properties [space]”),复制/粘贴文件名以确保
如果它们不存在,则IntelliJ设置错误。资源不会包含在您的目标构建中。使用IntelliJ创意在线查看如何执行此操作的教程。