Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5979489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:39:59+00:00 2026-05-22T21:39:59+00:00

I have a nasty problem referencing resources when using a Maven project and Jar

  • 0

I have a nasty problem referencing resources when using a Maven project and Jar files…

I have all my resources in a dedicated folder /src/main/resources which is part of the build path in Eclipse. Files are referenced using

getClass().getResource("/filename.txt")

This works fine in Eclipse but fails in the Jar file – there the resources are located in a folder directly below the jar’s root…

Does anyone know a ‘best practice’ to reference a file both in the JAR and (!) in Eclipse?

Edit:
The problem is that while the resources actually are located in the JAR in a folder “resources” at the top level, the above method fails to find the file…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T21:39:59+00:00Added an answer on May 22, 2026 at 9:39 pm

    The contents of Maven resource folders are copied to target/classes and from there to the root of the resulting Jar file. That is the expected behaviour.

    What I don’t understand is what the problem is in your scenario. Referencing a Resource through getClass().getResource("/filename.txt") starts at the root of the classpath, whether that (or an element of it) is target/classes or the JAR’s root. The only possible error I see is that you are using the wrong ClassLoader.

    Make sure that the class that uses the resource is in the same artifact (JAR) as the resource and do ThatClass.class.getResource("/path/with/slash") or ThatClass.class.getClassLoader().getResource("path/without/slash").

    But apart from that: if it isn’t working, you are probably doing something wrong somewhere in the build process. Can you verify that the resource is in the JAR?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have recently started using git and had a nasty problem when someone committed
I have a nasty problem. I want to get rid of a certain database
We have a nasty little problem. In short we can detect if a screen
I have encountered a rather nasty problem with the DataGridView control (Windows.Forms, .NET Framework
So I have a nasty stack overflow I have been trying to track down
My coworker and I have encountered a nasty situation where we have to use
At work, we have one of those nasty communal urinals. There is no flush
Have just started using Google Chrome , and noticed in parts of our site,
Have just started using Visual Studio Professional's built-in unit testing features, which as I
I've got this nasty problem where sending multiple, large messages in quick succession from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.