Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: Loading Resources: Maven Project Structure vs Jar

  1. #1
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default 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:
    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:
    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:
    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:
    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?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  2. The Following User Says Thank You to aussiemcgr For This Useful Post:

    cejot88 (August 13th, 2014)


  3. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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?
    If you don't understand my answer, don't ignore it, ask a question.

  4. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default 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.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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:
          System.out.println("user.dir="+System.getProperties().getProperty("user.dir"));
     
          System.out.println("cp="+System.getProperties().getProperty("java.class.path"));
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Loading Resources: Maven Project Structure vs Jar

    I replaced the user name with *** for privacy reasons
    IDE:
    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:
    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:
    ...target/classes/SpriteMap.xml
    ...target/classes/cop/rm/...(this is the starting directory for the java class files)
    Jar:
    resources/SpriteMap.xml
    cop/rm/...(this is the starting directory for the java class files)
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Loading Resources: Maven Project Structure vs Jar

    Here is the full file structure, starting from my project directory:
    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.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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?
    If you don't understand my answer, don't ignore it, ask a question.

  10. #9
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default 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.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #11
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default 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:
    <build>
         <resources>
              <resource>
                   <directory>src/main/resources</directory>
                   <targetPath>${project.build.outputDirectory}/resources</targetPath>
              </resource>
         </resources>
    </build>

    Then when it all worked as expected.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  13. The Following User Says Thank You to aussiemcgr For This Useful Post:

    cejot88 (August 13th, 2014)

  14. #12
    Junior Member
    Join Date
    Jun 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Loading Resources: Maven Project Structure vs Jar

    Quote Originally Posted by Norm View Post
    I'm moving this to the IDE section to see if anyone knows how to configure the IDE.
    Hi,
    so now you can access files under resources when running through IDE as well as jar?

    I am having same problem- I have file under src/main/resources/testdata and I need absolute path of file as I need to copy it to some other machine in my test.
    Can you please elaborate more, how you fix your problem?

    THanks

  15. #13
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Loading Resources: Maven Project Structure vs Jar

    Are you using Maven?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  16. #14
    Junior Member
    Join Date
    Aug 2014
    Posts
    1
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Loading Resources: Maven Project Structure vs Jar

    Just registered to thank you aussiemcgr
    I was searching for without any success. Tried so much from Stackoverflow, but nothin solves my problem till i found this

Similar Threads

  1. Jar project to use Derby Database
    By Ma_2 in forum JDBC & Databases
    Replies: 1
    Last Post: July 17th, 2012, 11:03 AM
  2. [SOLVED] FREE HELP WANTED: Setting up Class structure for Gui(JFrame) project.
    By JonLane in forum Object Oriented Programming
    Replies: 2
    Last Post: February 21st, 2012, 02:19 AM
  3. Loading different version of jar lib from servlet
    By testxyz in forum Java Servlet
    Replies: 2
    Last Post: November 2nd, 2011, 10:53 AM
  4. Replies: 15
    Last Post: September 2nd, 2011, 05:05 PM
  5. Access jar's resources within a servlet
    By RogerBotter in forum Java Servlet
    Replies: 2
    Last Post: July 14th, 2010, 10:18 AM