Sunday, 15 February 2015

java - Why I cant read properties file using getResourceAsStream when it is in classpath (under WEB-INF/lib) -


I was testing while using getResourceAsStream to read a resource file when the property file is in the field (web- Under INF \ lib folder). And it is not working for some reason. Does anyone tell me what I am doing?

This is my environment: the operating system is Windows 7. IDE Eclipse 4.2.3 is Kepler 64-bit. The server is running on the PATum 6.0.37 8081 port.

I have created a web application MyWebProject

I have the app.properties file which is WEB-INF \ Lib is located in the folder. This is the content of the app.properties file:

data source = jdbc / mydb

I created a class AppProperties is the class that reads this file:

  Public Ultimate Class AppProperties {Private Static String DATA_SOURCE_NAME; Public static string getDataSourceName () {return DATA_SOURCE_NAME; } Fixed {Classloader Classloader = Faucet; InputStream property file = null; Attribute Properties = Null; Final string PROPERTIES_FILE_NAME = "app.properties"; Properties = New Property (); ClassLoader = Thread curentThread (). GetContextClassLoader (); System.out.println ("AppProperties: class name loader =" + classLoader.getClass (). GetName ()); PropertiesFile = classLoader.getResourceAsStream (PROPERTIES_FILE_NAME); System.out.println ("AppProperties: propertiesFile =" + GIFTERS file); }}  

Then I created a Servlet AppPropertiesServlet to test it:

  Public category AppPropertiesServlet HttpServlet {public AppPropertiesServlet} {         Super(); } Secure zero code (HTTPArlet request requests, HTTPSvette response response) throws serviceal expedition, IOException {String dataSourceName = null; DataSourceName = AppProperties.getDataSourceName (); }}  

I have deployed MyWebProject web app on Tomcat, under a tokkat's webapp folder, a new folder MyWebProject was created with all the necessary files, then I have to run the sublett http: // localhost: 8081 / MyWebProject / AppPropertiesServlet and that is displayed on the Tomcat console: < / P>

AppProperties: name of classLoader = org Apache.catalina.loader.WebAppClassLoader
AppProperties: propertiesFile = null

Why propertiesFile is empty? In the app.properties file web-INF \ lib folder, there is a MyWebProject folder, but somehow ClassLoder is unable to find it. strange. Am I doing wrong here?

thanks

If you want it, WEB-INF / lib Under it is to be packed inside the jar.

If you want it to be like a normal file, it should be within Web-NF / Class .


No comments:

Post a Comment