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();
}
}
}