armani
armani

Reputation: 156

Java - loading resources directory

This is from Chapter 4 of the book "Core Java 9 for the impatient".

Resources can have subdirectories which can be relative or absolute. For example, MyClass.class.getResourceAsStream("/config/menus.txt") locates config/menus.txt in the directory that contains the root of the package to which MyClass belongs.

This is the confusing line - "the directory that contains the root of the package to which MyClass belongs." Can someone tell me which directory this line refers to ? An example would be nice.

Upvotes: 3

Views: 1244

Answers (1)

Software Engineer
Software Engineer

Reputation: 16130

Packages and folders

In normal Java, there is a one-to-one mapping of an apps packages and the file-system contained in a given jar file, of which root ('/') maps to the default (unnamed or root) package. The MyClass.class.getReourceAsStream() method loads resources from a path relative to the package MyClass is in, rooted at the location pointed to by the environment variable CLASSPATH (which usually points to where your jars are kept). So, if MyClass is actually com.mycompany.myapp.MyClass, then resources relative to this class are in the /com/mycompany/myapp/ directory.

Sources

In a standard java/maven project, maven would compile your java code from /src/main/java into a target directory and copy anything under src/main/resources into the same target directory structure. To locate resource source files in your application relative to the class com.mycompany.myapp.MyClass, you would look in the src/main/resources/com/mycompany/myapp/ folder. However, with your specific example, as @user207421 points out in the comment below, your path starts with a slash (/). This makes the path to the resource 'absolute', not 'relative' to your class. Your path starts at the root and thus the file needs to be in the directory /config in the jar. In a standard java/maven project, this file would be held in the directory /src/main/resources/config/.

Generated sources

It should be mentioned that even in a standard java/maven project it is possible for resource files to be sourced from directories other than src/main/resources. For example, it is common practice to generate code and resources into the target/generated-sources directory, and have those files compiled and copied into the resultant jar, and I've worked in some shops that care enough to generate sources and resources into separate directories under target, so they have target/generated-resources too, though for most projects this is considered overkill for generated sources. At assembly time, both the src/main/resources and the target/generated-resources directories should be copied into the root of the jar file.

Normal Java

Also, to emphasise the point I made at the start, this is only true for normal Java; that is, Java that follows the textbook approach to having .java text files from a filesystem define classes and having those classes 'compiled' into a jar file that is loaded by the runtime interpreter. Technically none of this is necessary and some implementations of java look nothing like this. IBM has in the past written a version of java where classes are loaded from databases rather than jar files, and if you add in runtime weaving and class generation then things can very quickly get really far from the 'normal' model described above.

Upvotes: 2

Related Questions