GetSystemResourceAsStream () returns null

Hiii ... I want to get the contents of a properties file into an object of class InputStream using getSystemResourceAsStream (). I created a sample code. It works well using the main () method, but when I deploy the project and run it on the server, the path to the properties file cannot be obtained ... therefore the input object is null.

Sample code here.

public class ReadPropertyFromFile { public static Logger logger = Logger.getLogger(ReadPropertyFromFile.class); public static String readProperty(String fileName, String propertyName) { String value = null; try { //fileName = "api.properties"; //propertyName = "api_loginid"; System.out.println("11111111...In the read proprty file....."); // ClassLoader loader = ClassLoader.getSystemClassLoader(); InputStream inStream = ClassLoader.getSystemResourceAsStream(fileName); System.out.println("In the read proprty file....."); System.out.println("File Name :" + fileName); System.out.println("instream = "+inStream); Properties prop = new Properties(); try { prop.load(inStream); value = prop.getProperty(propertyName); } catch (Exception e) { logger.warn("Error occured while reading property " + propertyName + " = ", e); return null; } } catch (Exception e) { System.out.println("Exception = " + e); } return value; } public static void main(String args[]) { System.out.println("prop value = " + ReadPropertyFromFile.readProperty("api.properties", "api_loginid")); } } 
+7
java classloader
source share
3 answers

i deployment of the project and launch on the server,

This is similar to the JSP / Servlet web application. In this case, you need to use ClassLoader , which is obtained as follows:

 ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 

This has access to the paths of the all path associated with the web application in question, and you no longer depend on which parent class loader (the web application has more than one!) Loaded your class.

Then on this class loader, you just need to call getResourceAsStream() to get the classpath resource as a stream, not getSystemResourceAsStream() , which depends on how the web application starts. You do not want to depend on this, since you do not control it on external hosting:

 InputStream input = classLoader.getResourceAsStream("filename.extension"); 

This is finally more robust than your original getSystemResourceAsStream() and Class#getResourceAsStream() approach, as others have suggested.

+15
source share

SystemClassLoader loads resources from the java.class.path of the witch maps into the CLASSPATH system variable. In your local application, you probably have the resource you are trying to load configured in the java.class.path variable. This is another story on the server, because most likely the server is loading your resources from another classloader.

Try using ClassLoader , which loaded the class using the correct path:

 getClass().getResourceAsStream(fileName); 

This article may also be helpful.

+6
source share

Try using getResourceAsStream() instead of 'getSystemResourceAsStream ()'.

+1
source share

All Articles