How to mark package as a resource folder?

前端 未结 2 1332
情书的邮戳
情书的邮戳 2020-12-09 18:47

I have a dir structure for Intellij 12:

   ...
   ...test
       - java
         - com.mycompany.myproject
           - package1 (contains code, etc,.)
              


        
相关标签:
2条回答
  • 2020-12-09 19:14

    Directories Creation

    Intellij creates directories when you ask her to create package. It is not an error.

    If you create package "com", it will create the dir "com", and if you create a source file there, it will think that the file is in the package "com".

    If you create package "com.next.pack", it will create three nested dirs "com", then "next", then "pack", and if you create a source file there, it will think that the file is in the package "com.next.pack".

    Directories Structures

    Only the path under the source root is taken as a package. Any directory(ies) can be set as a source root(s).


    Resources roots

    Make practically any structure of directories. Somewhere in it there is the root dir of resources. Right-click it and Mark Directory As ... Resources Root.

    Notice, the same method can be used for the directories structures for tests, test classes, and test resources. Look here.

    0 讨论(0)
  • 2020-12-09 19:29

    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.

    0 讨论(0)
提交回复
热议问题