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

  • SEARCH
  • Home
  • 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 142803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:59:41+00:00 2026-05-11T07:59:41+00:00

I have problems with class loaders. Sometimes it works, sometimes it doesn’t work. When

  • 0

I have problems with class loaders. Sometimes it works, sometimes it doesn’t work.

When I started, I have tested this works but not from *.jar:

URL url = AcAnalyzer.class.getResource("../stuff/resource");  // and this works even from jar file: URL url = Acnalyzer.class.getResource("/stuff/resource"); URL url = AcAnalyzer.class.getClassLoader().getResource("stuff/resource");  // But I got into the problem with tomcat..when I need to deploy it into the tomcat I had to do this: URL url = Thread.currentThread().getContextClassLoader().getResource("something.xml"); 

where something.xml has to be in WEB-INF/classes/

… url.getFile();

The problem is that most of the time it has to work within and not within jar at the same time. Now I have test where my class is getting resource, and the jar file is used in some project deployed under the tomcat.. and somehow it doesn’t want to work anymore:

I’m bit puzzled about class loaders. How to get this resource? And and the same time have working test.

URL url = Thread.currentThread().getContextClassLoader().getResource("com/st/resource"); FileInputStream inputStream = new FileInputStream(url.getFile()); 
java.io.FileNotFoundException: file:/home/aaa/.m2/repository/com/st/module-1.1-SNAPSHOT.jar!/com/st/resource (No such file or directory) 
  • 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. 2026-05-11T07:59:41+00:00Added an answer on May 11, 2026 at 7:59 am

    I’m not sure exactly what the question is in the first part, although I’d advise not using ‘../’ in a resource path.

    For the second part, if you want to load a resource you shouldn’t use FileInputStream – you should use getResourceAsStream() instead of getResource().getFile(). Just load from that InputStream – there won’t always be an individual file that you can load with FileInputStream.

    EDIT: The two ways of referring to a resource are ClassLoader.getResource and Class.getResource (and the equivalents with the AsStream suffix). The difference between them (the only difference that I’m aware of, although there may be others) is that Class.getResource treats the given path as being relative to the package of the class that you call it on. So

    ClassLoader.getResource('foo/bar/baz/test.xml') 

    is equivalent to

    foo.bar.SomeClass.class.getResource('baz/test.xml'); 

    As for the difference between Class.getClassLoader() and Thread.getContextClassLoader() I won’t claim to have a good understanding – I suggest you ask that as a separate question.

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

Sidebar

Related Questions

I've encountered some problems with MongoID. I have three models: require 'mongoid' class Configuration
I have a problem with getting a class from an external .jar file. I
I have some problems trying to get a bag collection of a class to
My class loader resolves resources to a source that does not have a url
I have question, maybe it's not critical in programming, but to me it's very
Good day, I have the following problem: class B extends class A and methods
I have a design problem: Question class: have a question and answer (String) TextQuestion
I have a problem with ViewHolder class. I use ViewHolder to improve my List
I have problem organizing my unittest based class test for family of tests. For
I have a problem to test my non activity-class which need the context of

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.