In above example, we created a simple project using IntelliJ IDE 15.0.2 and JDK 1.8.
Notice how resources are copied in output folder . Resources are loaded from output folder during runtime.
Output:
-----------------------------------------
using this.getClass().getResource
-----------------------------------------
-> attempting input resource: test-pkg-resource.txt
absolute resource path found :
C:/load-resource/out/production/load-resource/com/logicbig/example/test-pkg-resource.txt
file content: test file, local to package
-> attempting input resource: /test-pkg-resource.txt
no resource found: /test-pkg-resource.txt
-> attempting input resource: root-resource.txt
no resource found: root-resource.txt
-> attempting input resource: /root-resource.txt
absolute resource path found :
C:/load-resource/out/production/load-resource/root-resource.txt
file content: root test file
-----------------------------------------
using current thread context loader
-----------------------------------------
-> attempting input resource: test-pkg-resource.txt
no resource found: test-pkg-resource.txt
-> attempting input resource: /test-pkg-resource.txt
no resource found: /test-pkg-resource.txt
-> attempting input resource: root-resource.txt
absolute resource path found :
C:/load-resource/out/production/load-resource/root-resource.txt
file content: root test file
-> attempting input resource: /root-resource.txt
no resource found: /root-resource.txt
-----------------------------------------
using ClassLoader.getSystemClassLoader()
-----------------------------------------
-> attempting input resource: test-pkg-resource.txt
no resource found: test-pkg-resource.txt
-> attempting input resource: /test-pkg-resource.txt
no resource found: /test-pkg-resource.txt
-> attempting input resource: root-resource.txt
absolute resource path found :
C:/load-resource/out/production/load-resource/root-resource.txt
file content: root test file
-> attempting input resource: /root-resource.txt
no resource found: /root-resource.txt
Compiling with javac
By default javac compiles the java source files in the same directory. In above example if we run this command in Windows O.S:
C:\load-resource>javac com/logicbig/example/ClassA.java
The compilation will end up like this:
Now run 'java' from command line:
The output is same as we saw with IntelliJ project above because relative locations of the resource files are sill same from .class file.
Compiling with javac using the output folder option
The resources in this case are not copied at all. We have to copy them manually or better use some IDE or build tool like maven.
Resource loading in a Maven project
Maven by default has a standard directory structure. During build it only copies resources to '/target/classes' folder which were originally placed under src/main/resources directory. If resources are somewhere else they are ignored (this is default behavior which can be customized, we will explore that later).
Let's delete the previously created output folder and convert our above example to the maven project. We just need to add a pom and put all java sources under src/main/java (see quick tutorials on maven here):
We can see there are no resources copied because we have to place all our txt files in the 'resources' folder. Here's the complete maven project with all txt files in the 'resources' folder:
Dependencies and Technologies Used:
|