I have this code in a Java EE Application for reading the Properties file.
Even though the Myservice.properties is placed under WEB-INF/classes folder, the properties aren’t being read in Linux environment, but it is working fine in Windows environment.
InputStreamReader fMainProp = new InputStreamReader(this.getClass().getResourceAsStream("/Myservice.properties"));
Will the above will only work in windows?
MyWeb() {
prop = new Properties();
try {
InputStreamReader fMainProp = new InputStreamReader(this.getClass().getResourceAsStream("/Myservice.properties"));
prop.load(fMainProp);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
Whether that code works depends on if the classloader which loaded the calling class as represented by
getClass()in your code has access to the/WEB-INF/classes. Apparently the class in question is by itself not inside the/WEB-INF/classesor has a copy which is placed elsewhere in the classpath and server make/version used in the Linux environment uses a somewhat different classloader hierarchy than the server make/version used in the Windows environment.Fact is, if you can’t guarantee that the properties file is to be loaded by the same classloader as the calling class, then you should not try to get it by the classloader of the calling class, but by the context class loader of the current thread. It has access to everything.
Please note that with this class loader, the path cannot be relative. So don’t start with a leading slash.