在 IntelliJ 中为 Maven 项目添加资源
我有一个这样的项目结构:
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
InputStream is = getClass().getResourceAsStream("launchers.properties");
这是给空值.
在 Intellij 中,我无法在 src/main
下为 resources
文件夹添加新包,因此项目结构是这样的.如何将 launchers.properties
资源文件加载到项目中?
In Intellij I am not able to add a new package under src/main
for resources
folder so
that the project structure looks like this. How can I load the launchers.properties
resource file into the project?
src
|-main
|-java
|-com.abc.xyz
|-Login.java
|-resources
|-com.abc.xyz
|-Login
|-launcher.properties
我尝试了@maba 建议的解决方案,但仍然无法正常工作
I tried the solution suggested by @maba but still not working
launcher.properties
不应位于名为 Login
的文件夹下.它应该直接放在src/main/resources/com/abc/xyz
文件夹中.
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.
这真的像我说的一样简单,但如果资源文件夹没有标记为源文件夹,那么这可能是问题所在.
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 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:
并且重新运行应用程序不会返回任何 null
值:
And rerunning the application will not give you any null
value back:
并且包视图肯定也会显示 launchers.properties
文件:
And the package view will surely show the launchers.properties
file as well: