-1

I am not able to load the properties file into a java file. getting file not found exception. Can you please help here.

Java file location: classes/com/my/location/for/javabased/utilities/convertor/servlet/GetProp.java

Properties file location: classes/com/my/property/properties/Config.properties

My code:

Properties inputParams = new Properties();
FileInputStream in = new           FileInputStream("classes/com/my/property/properties/Config.properties");           
                inputParams.load(in);
                in.close(); 

Getting File not found exception

user3766874
  • 793
  • 2
  • 6
  • 14

2 Answers2

0

I prefer reading the property file in class like this

public class SomeClass{
private static Properties someProperties = new Properties();
static{
someProperties.load(SomeClass.class.getResourceAsStream("/com/my/property/properties/Config.properties"));
}

Hope this helps.

Vijendra Kumar Kulhade
  • 2,217
  • 3
  • 15
  • 25
0

The java class File (which is used by FileInputStream) is based on the file system path, either absolute, or relative to the current working directory. Both is mostly not under full control of the running application. If your resources can be found within the classpath (which from the point of view of the running application is always the same) you should use the resource loading mechanisme of your classloader (as Vijendra Kulhade pointed out in his answer).

Heri
  • 4,368
  • 1
  • 31
  • 51