Loading Resources: Maven Project Structure vs Jar
I have a problem which I can't find much help for on google. My project is a Maven project. For those unfamiliar, the Maven project structure is basically this:
Quote:
src/main/java
src/main/resources
...
Java code is placed in the java folder and resources (such as images) are placed in the resource folder.
We have several files, which are in the resource directory, such as:
Quote:
src/main/resources/SpriteMap.xml
src/main/resources/images/spriteImages.xml
src/main/resources/images/character/player/headmovements.jpg
...
Now, when you package the project into a jar, the file structure changes a bit:
Quote:
resources/SpriteMap.xml
resources/images/spriteImages.xml
resources/images/characters/player/headmovements.jpg
You'll notice the src/main no longer exists.
So, to access a resource within a jar, I use the code:
Code java:
URL stream = getClass().getResource("/resources/SpriteMap.xml");
That same line of code does NOT work within my IDE (Eclipse). Within Eclipse, it sets stream to null, which then throws a null pointer later on for obvious reasons. Based on what I've read online, the reason is because that first slash is supposed to take you to the resource root. However, the resource root is different in Eclipse than it is in a Jar (src/main does not exist in the Jar), so it just doesn't work.
While I can find a lot of stuff from people saying what the problem is, I have been unable to find an actual solution. Does anyone have any ideas?
Re: Loading Resources: Maven Project Structure vs Jar
If this is a question about how to use an IDE, should it be in the IDE section?
Can the classpath be set?
What happens without the leading /? what is the name of the package the code is in?
Re: Loading Resources: Maven Project Structure vs Jar
Technically, this is a Maven question. As far as I know, the Maven Project Structure doesn't change based on IDE.
I'm not sure how I can set the classpath in this situation. I'll have to do a bit of research and get back to you.
Without the leading /, the URL stream is set to null on both the Jar and the IDE.
Re: Loading Resources: Maven Project Structure vs Jar
If the code works in the jar and not in the IDE,
it seems like its an IDE problem. If the IDE set the classpath to include the folder with the resources folder it should work.
What is the classpath when the program executes in the IDE?
Execute this to see:
Code :
System.out.println("user.dir="+System.getProperties().getProperty("user.dir"));
System.out.println("cp="+System.getProperties().getProperty("java.class.path"));
Re: Loading Resources: Maven Project Structure vs Jar
I replaced the user name with *** for privacy reasons
IDE:
Code :
user.dir=C:\Users\***\git\running-man-desktop-version\cop.rm.desktop
cp=C:\Users\***\git\running-man-desktop-version\cop.rm.desktop\target\classes;C:\Users\***\git\running-man-core\cop.rm.core\target\classes;C:\Users\***\git\running-man-core\cop.rm.core\target\test-classes;C:\Users\***\.m2\repository\org\mockito\mockito-all\1.9.5\mockito-all-1.9.5.jar;C:\Users\***\.m2\repository\junit\junit\4.0\junit-4.0.jar;C:\Users\***\Downloads\springsource\sts-2.9.1.RELEASE\plugins\org.junit_4.8.2.v4_8_2_v20110321-1705\junit.jar;C:\Users\***\Downloads\springsource\sts-2.9.1.RELEASE\plugins\org.hamcrest.core_1.1.0.v20090501071000.jar
Jar:
Code :
user.dir=C:\Users\***\Desktop
cp=RunningManDesktop.jar
EDIT:
I just checked the target folder mentioned in the classpath. I found that the target folder contains the files, but they are not stored in a separate resources folder.
For example:
IDE:
Quote:
...target/classes/SpriteMap.xml
...target/classes/cop/rm/...(this is the starting directory for the java class files)
Jar:
Quote:
resources/SpriteMap.xml
cop/rm/...(this is the starting directory for the java class files)
Re: Loading Resources: Maven Project Structure vs Jar
What folder is the src folder in? Is that folder at the end of any of the paths on the classpath?
Re: Loading Resources: Maven Project Structure vs Jar
Here is the full file structure, starting from my project directory:
Code :
cop.rm.desktop/.classpath
cop.rm.desktop/.gitignore
cop.rm.desktop/.project
cop.rm.desktop/pom.xml
cop.rm.desktop/.settings
cop.rm.desktop/.settings/org.eclipse.jdt.core.prefs
cop.rm.desktop/.settings/org.eclipse.m2e.core.prefs
cop.rm.desktop/src
cop.rm.desktop/src/main
cop.rm.desktop/src/main/java
cop.rm.desktop/src/main/java/cop
cop.rm.desktop/src/main/java/cop/rm
cop.rm.desktop/src/main/java/cop/rm/runner
cop.rm.desktop/src/main/java/cop/rm/runner/MainExecutable.java
cop.rm.desktop/src/main/java/cop/rm/runner/ViewPanel.java
cop.rm.desktop/src/main/resources
cop.rm.desktop/src/main/resources/SpriteMap.xml
cop.rm.desktop/src/main/resources/images
cop.rm.desktop/src/main/resources/images/spriteImages.xml
cop.rm.desktop/src/main/resources/images/character
cop.rm.desktop/src/main/resources/images/character/player
cop.rm.desktop/src/main/resources/images/character/player/...[image files here]...
cop.rm.desktop/src/main/resources/images/level
cop.rm.desktop/src/main/resources/images/level/...[image files here]...
cop.rm.desktop/src/main/resources/images/obstacle
cop.rm.desktop/src/main/resources/images/obstacle/crate
cop.rm.desktop/src/main/resources/images/obstacle/crate/Crate.png
cop.rm.desktop/src/test
cop.rm.desktop/src/test/java
cop.rm.desktop/src/test/java/...[empty folder]...
cop.rm.desktop/src/test/resources
cop.rm.desktop/src/test/resources/...[empty folder]...
cop.rm.desktop/target
cop.rm.desktop/target/desktop-0.0.1-SNAPSHOT
cop.rm.desktop/target/classes
cop.rm.desktop/target/classes/SpriteMap.xml
cop.rm.desktop/target/classes/cop
cop.rm.desktop/target/classes/cop/rm
cop.rm.desktop/target/classes/cop/rm/runner
cop.rm.desktop/target/classes/cop/rm/runner/MainExecutable.class
cop.rm.desktop/target/classes/cop/rm/runner/MainExecutable$1.class
cop.rm.desktop/target/classes/cop/rm/runner/MainExecutable$2.class
cop.rm.desktop/target/classes/cop/rm/runner/ViewPanel.class
cop.rm.desktop/target/classes/images
cop.rm.desktop/target/classes/images/spriteImages.xml
cop.rm.desktop/target/classes/images/character/player
cop.rm.desktop/target/classes/images/character/player/...[image files here]...
cop.rm.desktop/target/classes/images/level
cop.rm.desktop/target/classes/images/level/...[image files here]...
cop.rm.desktop/target/classes/images/obstacle
cop.rm.desktop/target/classes/images/obstacle/crate
cop.rm.desktop/target/classes/images/obstacle/crate/Crate.png
cop.rm.desktop/target/classes/META-INF
cop.rm.desktop/target/classes/META-INF/MANIFEST.MF
cop.rm.desktop/target/classes/META-INF/maven
cop.rm.desktop/target/classes/META-INF/maven/cop.rm
cop.rm.desktop/target/classes/META-INF/maven/cop.rm/desktop
cop.rm.desktop/target/classes/META-INF/maven/cop.rm/desktop/pom.properties
cop.rm.desktop/target/classes/META-INF/maven/cop.rm/desktop/pom.xml
cop.rm.desktop/target/maven-archiver
cop.rm.desktop/target/maven-archiver/pom.properties
cop.rm.desktop/target/surfire
cop.rm.desktop/target/surfire/...[a bunch of surefire temp files]...
cop.rm.desktop/target/test-classes
cop.rm.desktop/target/test-classes/...[empty folder]...
If you are asking about the class paths in my previous post: no. All the classpaths there are pointing to the target folders. However, as you will see above, the images are copied over into the target folder by the compiler.
Re: Loading Resources: Maven Project Structure vs Jar
What I'm trying to determine is where the classpath points when the IDE executes because that is where getResource() looks. If the resources folder is in a folder on the classpath its contents should be found.
See where the classpath is when the IDE executes and see where the resources folder is.
How can the IDE's classpath be changed so it points to the main folder?
Re: Loading Resources: Maven Project Structure vs Jar
It would appear to me that the IDE's classpath is pointing to the target folder. In the target folder, the images are not located in it's own resource folder, like it is in the jar. My only assumption from that is that when the jar is created, it builds it off the resource folder in source, which doesn't entirely make sense to me.
Here's the main thing to take out of this I guess:
The resource folder exists in the jar's classpath, but not the IDE's classpath.
One thing I tried to do is configure src/main/resources's output folder from:
cop.rm.desktop/target/classes
to:
cop.rm.desktop/target/classes/resources
But it complained that it: Cannot nest output folder 'cop.rm.desktop/target/classes/resources' inside output folder 'cop.rm.desktop/target/classes'
I assume that is because 'cop.rm.desktop/target/classes' is the default output folder. Based on what I understand from you here, if I somehow managed to get it to create a resources folder in the default output folder (which, for all intensive purposes, seems to be the runtime classpath) and add the images into that folder, the issue would hopefully be resolved, since the jar and the IDE would both have a resources folder in it's runtime classpath.
Re: Loading Resources: Maven Project Structure vs Jar
I'm moving this to the IDE section to see if anyone knows how to configure the IDE.
Re: Loading Resources: Maven Project Structure vs Jar
I managed to resolve this issue. I will describe my solution in case someone comes across this thread in a google search. This was not an IDE problem, but rather a Maven issue.
It was actually very simple. In the project's pom file, I had to add:
Code :
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<targetPath>${project.build.outputDirectory}/resources</targetPath>
</resource>
</resources>
</build>
Then when it all worked as expected.