I’m trying to build an application that builds a resource file into a jar, but I’d like to have the project runnable within eclipse. I have a basic maven 2 structure for my project, and I’m unsure how to read in the file such that it’s found and used when run from the JAR or from within eclipse. Thought?
Structure:
src/main/java
src/main/resources/file.txt
Current reading method:
getClass().getResourceAsStream("/file.txt")
Is there reading method that will pick up src/main/resources/*, as well as the root level of the JAR (where resources are deployed)?
I don’t understand your problem. Resources from
src/main/resourcesare automatically copied over totarget/classesand are thus available on the classpath under Maven and Eclipse relatively to the root level at the same location (unless your Eclipse project is not properly configured).And when packaged inside a JAR, the content of
target/classesis packaged “as is” so nothing is changed.In other words, accessing your
file.txtlike this is perfectly fine (and this is actually how things are documented):If you have a problem somewhere, please clarify.
Update: I did a quick test with the maven-eclipse-plugin and I can’t reproduce your problem:
The directory
src/main/resourcesis added as source folder as expected. Can you show your POM (especially theresourceselement if you define one)?