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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T20:18:42+00:00 2026-06-16T20:18:42+00:00

I am trying to read a config file based on the code here: http://www.opencodez.com/java/read-config-file-in-java.htm

  • 0

I am trying to read a config file based on the code here:

http://www.opencodez.com/java/read-config-file-in-java.htm

So I found that if the config.cfg is in same directory as of where I am running the code, then everything is fine but if the config is at different directory

example: /path/to/config.cfg

I get this error:

java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Properties.java:418)
at java.util.Properties.load0(Properties.java:337)
at java.util.Properties.load(Properties.java:325)
at packagename.conf.Config.<init>(Config.java:14)
at packagename.conf.Config.main(Config.java:30)

My guess is it is not able to find the file.
But how do I modify the above code to read config file from a different folder?
Thanks

Edit: Code from the link:

import java.util.*;
import java.util.Properties;

public class Config 
{
   Properties configFile;
   public Config()
   {
    configFile = new java.util.Properties();
    try {           
      configFile.load(this.getClass().getClassLoader().
      getResourceAsStream("myapp/config.cfg"));         
    }catch(Exception eta){
        eta.printStackTrace();
    }
   }

   public String getProperty(String key)
   {
    String value = this.configFile.getProperty(key);        
    return value;
   }
}
  • 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-06-16T20:18:44+00:00Added an answer on June 16, 2026 at 8:18 pm

    The code that you posted expects the configuration-file to be on the classpath (that is, in the same sorts of places that Java looks for your .class files). So, you can either include the directory containing the configuration-file in the classpath:

    java   -classpath .:/path/to   packagename.conf.Config
    

    Or else you can modify the code to expect the configuration-file to be a regular filesystem file:

    final InputStream cfg = new FileInputStream("/path/to/config.cfg");
    try
        { configFile.load(cfg); }
    finally
        { cfg.close(); }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to read in an XML file that I have saved to
I'm trying to use a config file that holds a list of hosts/websites and
Below is my code & xml file. I am trying to read a xml
I'm trying to read in a multi-line config file with records into a perl
I am trying to set the java util logging config file at runtime to
I am trying to read a config file which has some key value pairs
I am trying to read from a config file in ProgramFiles/MyApp but in Windows
I'm trying to create classes to read from my config file using ConfigurationSection and
What I am trying to do is read a list from a config file:
Trying to read headers for a csv file with: reader = csv.DictReader(open(PATH_FILE),skipinitialspace=True) headers =

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.