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 6669695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:10:50+00:00 2026-05-26T03:10:50+00:00

I am trying to read a properties file in my java web application. I

  • 0

I am trying to read a properties file in my java web application. I have tried these solution:

Where to place and how to read configuration resource files in servlet based application?

Howto access properties file from Java EE web application?

But none of them worked for me.

Here is the structure of my app:

enter image description here

The code that reads the properties file is placed in the A class and it did not work even I put the absolute path. A is a normal Java class.
But everything worked like a charm if the reading properties code is place in the servlet class (ProcessRequest.java)

Here is the code I have used:

public class A {
    public A() {
        try {
            Properties p = new Properties();
            p.load(this.getClass().getClassLoader().getResourceAsStream("/a.properties"));
            String n = p.getProperty("name");
            System.out.println("name: " + n);
        } catch (Exception ex) {
            Logger.getLogger(A.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

Any idea?

  • 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-26T03:10:51+00:00Added an answer on May 26, 2026 at 3:10 am

    You’ve put it in the servlets package, however you’re trying it to get from the classpath root. The leading / makes the path relative to the classpath root.

    Fix the path accordingly:

    p.load(this.getClass().getClassLoader().getResourceAsStream("/servlets/a.properties"));
    

    or, assuming that the current class is in servlets package already:

    p.load(this.getClass().getClassLoader().getResourceAsStream("a.properties"));
    

    Unrelated to the concrete problem, might it later happen that you move the properties file outside the WAR to an external location which allows easy editing of the file without the need to rebuild/redeploy everytime, then I’d suggest to use the thread’s context class loader instead of the current class’ class loader. It’ll work in all circumstances:

    p.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("servlets/a.properties"));
    

    (note that the path doesn’t need to start with / here, because it’s always relative to classpath root)

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

Sidebar

Related Questions

I have the following code trying to read a properties file: Properties prop =
I'm trying to read a java multiline i18n properties file. Having lines like: messages.welcome=Hello\
I've added a human-readable configuration file to my app using java.util.Properties and am trying
HI, I have a typical messages.properties file in my application. I'm trying to generate
I am trying to read data from properties file.I have specified the path in
I am trying to read a single file from a java.util.zip.ZipInputStream , and copy
I'm trying to figure out how to read the META-INF/MANIFEST.MF file form a java
I'm trying to find out how to read/write to the extended file properties in
I'm trying to read a properties file /res/version.num . But all I get is
I am trying to read Japanese string values from a .properties file with the

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.