3

I have spent last 2 hours just to add a properties file in my IntelliJ IDEA classpath. I am an Eclipse user and find it embarrassing that I am unable to do it in IDEA. I have a .properties file in my IntelliJ IDEA project and it's added in the classpath but still at runtime I am getting exception that file is not present.

I have followed all the answers in this question Add a properties file to IntelliJ's classpath

I have done the following things:-

1) Go to Project Structure. Select your module. Find the folder in the tree on the right and select it. Click the Sources button above that tree (with the blue folder) to make that folder a sources folder.

2)I have checked that settings->compiler->resource patterns has entry for ?*.properties

3) I have added the below tag in my pom.xml

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
        </resource>
    </resources>
</build>

Can someone help me here?

Further update. It's a maven web application project. I am trying to access the .properties file in my servlet. My servlet sits in src/main/java/some_package and my .properties file sits in src/main/resources/some_package.

try {

        Properties prop = new Properties();
        InputStream input = null;

        try {

            input = new FileInputStream("package\\myProperty.properties");

            // load a properties file
            prop.load(input);

            // get the property value and print it out
            System.out.println("reading the property file " );
            System.out.println("prop1 =" + prop.getProperty("prop1"));
            System.out.println("prop2 = " + prop.getProperty("prop2"));


        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
Community
  • 1
  • 1
user911
  • 1,509
  • 6
  • 26
  • 52
  • can someone please help here. This shouldn't be this hard :( – user911 Feb 24 '14 at 23:17
  • Could you provide some more information about the error and how do you use your `properties` file in runtime. I mean some code samples, configs and exception stack trace. – Eugene Evdokimov Feb 25 '14 at 09:27
  • @EugeneEvdokimov - I have added more details. Please check. – user911 Feb 25 '14 at 14:51
  • If you use **Maven** to build a project, the result should not depend on the IDE in which you work, whether it is Eclipse or IDEA. There are plenty of good answers to your question on SO: http://stackoverflow.com/a/2308388/546117, http://stackoverflow.com/a/12523396/546117, http://stackoverflow.com/a/18508032/546117. Hope they will be helpful. – Eugene Evdokimov Feb 25 '14 at 20:08
  • Thanks @EugeneEvdokimov but the issue is that my code is not directly loading this property file. I am calling some third party jar which in turn is trying to load this property file. So I can't really try to change the way the file is loaded. The only thing that I need to make sure is that it should be there on the classpath. For the time being I have added it in the lib folder of tomcat but I need a permanent solution to this problem. – user911 Feb 25 '14 at 20:23

2 Answers2

1

I found that you should not mention .properties while looking for the file name IF file is say abc.properties then just look for abc instead of abc.properties

Arjuna
  • 11
  • 1
0

Generally, marking the right directories as source ones does solve the problem, but I found there's another important detail. You need to make sure that your working directory (can be found in Run Configurations) is set correctly. In my project it was set to the wrong directory for some reason, and since Java tries to find the *.properties file in the working directory (unless you specify an absolute path in your code), the client.properties in my properties wasn't found - it just wasn't present in the "wrong" working directory.

P.S. I'm a junior dev with little experience and this is my first answer here, so I may not have described everything perfectly and/or correctly, but I spent 2+ hours trying to figure out why my properties file wasn't loading, so I hope this helps someone who's in the same predicament. Happy coding :)

Dariia
  • 11