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
1.0k views
in Technique[技术] by (71.8m points)

intellij idea - How to mark package as a resource folder?

I have a dir structure for Intellij 12:

   ...
   ...test
       - java
         - com.mycompany.myproject
           - package1 (contains code, etc,.)
           - resourcePackage (want to contain .json files but can't mark as a resource)
             - myOtherJunk.json
           - o o o
       - resources
         - aResource.json

The thing is if I right click on my package name (com.mycompany.myproject) I can only add packages and not directories (like that of an existing resource folder).

However, I don't want to use that existing resource folder for the .json files that I'm going to read into per my test class.

So, I need something to support:

// this already works for the resources directory per the .json file but doesn't for the 
// myOtherJunk.json per the resourcePackage.
InputStream is = MyClassTest.class.getResourceAsStream("aResource.json");
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

This can be solved in several ways. An example of a good approach would be the following folder structure:

src
  main
     java
     resources
  test
     java
     resources

When this is done, you put all you java classes under src/main/java/com.mycompany package and any resources under /src/main/resources/com/mycompany folder.

To link them together, go to the project properties, and find the Path tab. Mark the src/main/java and src/main/resources as source folders. (see the screen-shot attached) Path tab

If you link them together, you'll be able to use getResourceAsStream() method. If you wonder why you should use the following folder structure - this is standard maven way of keeping things clean and tidy.


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

1.4m articles

1.4m replys

5 comments

57.0k users

...