Search code examples
javafilepropertiesclassloader

Not able to get resource, classloader and class returns target path


I am trying to get a properties file from /src/main/resources/properties/ but for some reason the following code returns the path of target classes instead of src files. Can you please help?

System.out.println(PropTest.class.getResource("/properties/app.properties"));
System.out.println(PropTest.class.getClassLoader().getResource("properties/app.properties"));
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
System.out.println(classloader.getResource("properties/app.properties"));
PropTest prop=new PropTest();
System.out.println(prop.getClass().getResource("/properties/app.properties"));

Each line gives the same output which is:

file:/C:/Users/b./eclipse-workspace/ordermonitoring/target/classes/properties/app.properties
file:/C:/Users/b./eclipse-workspace/ordermonitoring/target/classes/properties/app.properties
file:/C:/Users/b./eclipse-workspace/ordermonitoring/target/classes/properties/app.properties
file:/C:/Users/b./eclipse-workspace/ordermonitoring/target/classes/properties/app.properties

Solution

  • The behavior is correct. The resources will be loaded from class path. There wont be any src folder at runtime. What you can do is make the /src/main/resources as a source folder (if you are not using maven) so that the properties will be copied to target folder.