1

I have the following project structure:

ProjectName/src/java/com/main/Main.java

And I have a properties file in the following folder:

ProjectName/config/settings.properties

Now I tried to load the properties file in the Main.java with:

InputStream input = Main.class.getResourceAsStream("/config/settings.properties");
Properties prop = System.getProperties();
prop.load(input);

But this does not work. How is it the right way to do it?

Edit: I got the following error:

Exception in thread "main" java.lang.NullPointerException
    at java.util.Properties$LineReader.readLine(Unknown Source)
    at java.util.Properties.load0(Unknown Source)
    at java.util.Properties.load(Unknown Source)

Edit2: Now it works with eclipse. I did the following (adapted from getResourceAsStream() is returning null. Properties file is not loading)

1) This directory [config] must be put into the "build path". Right-click the directory in the Package Explorer or Project Explorer view, select "Build Path", then "Use as Source Folder". Note: This build path will be the class path for the project, when you run it.

2) As the config directory now is part of your class path and contains your properties file, you can simply load it with InputStream input = Server.class.getClassLoader().getResourceAsStream("settings.properties");

This works well for eclipse but not yet for a jar. I think I also have to add the build path somehow to the ant script.

How can I do that?

Edit3: If I take the settings.properties out of the config folder in the jar, then it works. Why? I want it in the config folder in the jar too.

Community
  • 1
  • 1
machinery
  • 5,972
  • 12
  • 67
  • 118
  • 2
    It would be helpful if you can let us know the error you are facing. We will be able to help you better. – Amal Gupta Oct 14 '14 at 10:10
  • What error is returned? – Rafa Romero Oct 14 '14 at 10:10
  • @afzalex It does still not work. – machinery Oct 14 '14 at 10:23
  • The output is: C:\Users\myName\workspace\ProjectName. Could it be the problem that the config is not in the classpath? I.e. my main class is in ProjectName/src/java.... but my config is in ProjectName/config... so they do not share the src folder. – machinery Oct 14 '14 at 10:38
  • I am sorry, I have not marked that you were using `Main` class as reference. but consider my answer and I am sure it will work. (I deleted my comments because the thing I told about calling of class is not in the case when a class is being taken as reference). – afzalex Oct 14 '14 at 10:45
  • Now it is weared. You have made your question an [xyproblem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – afzalex Oct 14 '14 at 13:02

4 Answers4

3

Use following :

InputStream input = Main.class.getResourceAsStream("../../../../config/settings.properties");
Properties prop = System.getProperties();
prop.load(input);

By adding ../ I am getting 1 step backward in your filesystem. In your code you were using /config....., which means C:/config..... (if it is in C drive).

The following will also work in your case. (but not in zipped file)

InputStream input = new FileInputStream("config/settings.properties");
Properties prop = System.getProperties();
prop.load(input);
afzalex
  • 8,598
  • 2
  • 34
  • 61
  • What is `Server` in your project? `Server.class.getClassLoader().getResourceAsStream("settings.properties");` is equivalent to `Server.class.getResourceAsStream("settings.properties");` – afzalex Oct 14 '14 at 12:54
  • Why you think it will work in jar, explain. Jar file will be created by IDE, and it will not contain that config folder in jar file. IDE will create the jar file as specified by `ANT`. You should not put these file anywhere, src folder is there for you. – afzalex Oct 14 '14 at 13:00
  • And if you still want to run it your way then you can add the classpath of the `properties` file. But remember it will not work on other computers or any place where these classpath has not been set. And I already told you are using wrong method – afzalex Oct 14 '14 at 13:10
1

getResourceAsStream find files relatively compiled (class) files on in jar file. Example: Your compiled files are in bin directory than you should put properties file to bin/config/settings.properties for give access to it.

WidWing
  • 176
  • 1
  • 10
  • I compile and build jar with ant. In the jar I have the following structure at the end myjar.jar/java/com/main/Main.java and myjar.jar/config/settings.properties. – machinery Oct 14 '14 at 10:34
  • try to put `settings.properties` to `myjar.jar/java/config`. – WidWing Oct 14 '14 at 10:39
1

You are using maven. So follow the maven structure and keep your resources like prop, xml etc files in resource folder and then call.

So folder structure would be like this

src/main/resource|
                 |
                  config|
                        |
                        settings.properties

InputStream input = Main.class.getClass().getResourceAsStream("/config/settings.properties");
Deepu--Java
  • 3,742
  • 3
  • 19
  • 30
  • I'm not using maven, I'm using ant to compile and generate a jar. – machinery Oct 14 '14 at 10:25
  • With InputStream input = Main.class.getClass().getResourceAsStream("/config/settings.properties"); I'm getting the same error. By the way, I'm trying it to run in eclipse not jar but I want to run it with both eclipse and as a jar. – machinery Oct 14 '14 at 10:28
1

Three things

  1. You need add the settings.properties to a jar (can be the same jar as your Main)
  2. Your classpath should include this jar
  3. You need to use the context Class Loader to load the properties file in you Main.

Classloader cl = Thread.currentThread().getContextClassLoader();

cl.getResourceAsStream("settings.properties");

Roshith
  • 2,116
  • 13
  • 21
  • You can add to the classapath in ANT using include ------------------------ ----------------------------------------------------------- – Roshith Oct 15 '14 at 06:29