¿Cómo marcar el paquete como una carpeta de recursos?

I have a dir structure for Intellij 12:

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

preguntado el 12 de febrero de 14 a las 08:02

3 Respuestas

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


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 carpeta.

To link them together, go to the project properties, and find the Path tab. Mark the src/main/java y 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.

Respondido 12 Feb 14, 08:02

This "project settings" menu described by @Funktik is in the "Project Structure Menu" under the File menu (at least in IntelliJ 15). - Timoteo Steele

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. Mire aquí.

respondido 16 nov., 20:17

In Android studio right click does not give the option Mark Directory As. There you can left press the directory and ctrl + shift + a y la búsqueda de Mark directory as and then press the right option. - Klyner

@Kylner, the shortcuts can be set in any way. Obviously, the default setting had changes since 2016 :-) - gangnus

Please use @ContextConfiguration annotation to load the resource files. Please see below example. @ContextConfiguration( { "/app-config.xml", "/test-data-access-config.xml",application-test.yml })

respondido 25 mar '22, 10:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.