Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
325 views
in Technique[技术] by (71.8m points)

java - Adding resources in IntelliJ for Maven project

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");

This is giving null.

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

I tried the solution suggested by @maba but still not working

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 is the initial class and setup:

enter image description here

Now create the resources folder:

enter image description here

enter image description here

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:

enter image description here

Now you'll be able to add packages to it:

enter image description here

enter image description here

And now you can add the file to it:

enter image description here

enter image description here

And rerunning the application will not give you any null value back:

enter image description here

And the package view will surely show the launchers.properties file as well:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...