How to load files/properties from WEB-INF directory? - java

It seems that in my Tapestry app, I can't load ini files nor properties file from WEB-INF directory or class path.
I tried several different methods which should load my file but non of them worked.
ex
realm.setResourcePath("/WEB-INF/auth.properties");
ex
realm.setResourcePath("classpath:wip/pages/auth.properties");
I need to load properties/ini file in order to use tapestry-security module which is based on Shiro.
Thanks for help !

Try ServletContext.getResourceAsStream("/WEB-INF/auth.properties") or ServletContext.getResourceAsStream("WEB-INF/auth.properties")
ServletContext has to be use from servlet, servletListener etc.

The root of the classpath is the way to go.
Put your file in src/main/resources/auth.properties then set your resourcePath using
realm.setResourcePath("classpath:auth.properties");
Check the ExtendedPropertiesRealm and the tapestry-security testapp for an example
http://svn.codehaus.org/tynamo/trunk/tapestry-security/src/test/java/org/tynamo/security/testapp/services/AppModule.java
http://svn.codehaus.org/tynamo/trunk/tapestry-security/src/test/resources/shiro-users.properties

Try
Properties props = new Properties();
props.load(new FileInputStream(new File(req.getServletContext().getRealPath("/WEB-INF/fileName.properties"))));
System.out.println(props);

I found the easiest way was to
put file in src/main/resources/config.properties. This will be put in /WEB-INF/classes/config.properties when the project is compiled by maven into WAR
read the file from a servlet with the following
InputStreaminputStream = getClass().getClassLoader().getResourceAsStream("config.properties");
https://crunchify.com/java-properties-file-how-to-read-config-properties-values-in-java/

Related

Loading Properties from a JAR file (java 1.6)

I manually inject a properties file inside a jar.
How to load properties from a jar file before java 1.7 ?
I tried many workarounds and nothing worked so far.
There's plenty questions about it, but everything is focused on ClassLoader methods from java 1.7.
When you have a properties file inside your classpath or inside your jar file it becomes a resource. Any other case is a simple file.
What you need to do, before you package your jar file, is add to your classpath the folder where the properties files are (i.e myproject/src/main/resources/) then wherever you do a
Properties properties = new Properties();
properties.load(MyClass.class.getResourceAsStream("/yourPropsFileName"));
it will load it!
Although, if you are using an external property file you can also load it by using:
Properties properties = new Properties();
properties.load(new FileInputStream("extenalPropsFileLocation"));
Hope it helps!
From some class, call:
getClass().getResourceAsStream("/path/to/props.props")
Make sure that the path matches up with a classpath location.

Path not found for a property file in Web application?

My Property file location is WebRoot/WEB-INF/Test/property files/Question.properties
Now I want to load the property file using FileInputStream. So I wrote the below code.
Properties pr = new Properties();
pr.load(new FileInputStream("WebRoot/WEB-INF/Test/property files/Question.properties"));
By the above code I got java.io.FileNotFoundException.I am using myeclipse for my development. Can any one suggest the path to read my properties.
You are opening it relative to the current directory. Do you know what the current directory is? Try creating a:
File f = new File("WebRoot/WEB-INF/Test/property files/Question.properties");
Then printing or debug the absolute path of the File object. This will tell you what file you are actually trying to open (i.e. full path).
However, if you are trying to open a 'resource' bundled with your web app (as suggested by /WEB-INF/ being in the path) this is probably not a good way to do it. One alternative is to build your 'resource' into one of your application's .jar files.
See here, for a related answer:
Refer to a web page inside a jar file
use the method InputStream java.lang.Class.getResourceAsStream(String name) instead, this will work when you run your class within eclipse and outside eclipse. and use the same path as what you have mentioned in your code and append "/" at the front.
Hope this helps !
Do one thing right click on Question.properties file click on the properties get the Location of the file.
Properties pr = new Properties();
pr.load(new FileInputStream("Location of the file"));

Unable to load application.properties file

What path should I be putting in here to load this .properties file? I can't seem to get it to load...
InputStream stream = ProductVersionService.class.getResourceAsStream("/application.p‌​roperties");
properties.load(stream);
I could see, you are using Maven to build this project and hence your final jar wont have src/main/resource or src/main/java either. All of these directories will be removed and all the contents beneath it will be added to root of jar and hence referring the path using src/main/* will result into an error. You should make use of
Thread.currentThread().getContextClassLoader().getResourceAsStream("application.properties")
You need to specify the full path to the properties file, from the root through the file itself: /main/resources/application.properties
Properties prop = new Properties();
prop.load(PropertiesUtil.class.getClassLoader().getResourceAsStream("/main/resources/application.properties"));
Since your properties files will be read from war package, easiest solution would be to load them as a ResourceBundle:
ResourceBundle resourceBundle = ResourceBundle.getBundle("main.resources.application");
String prop = resourceBundle.getString("nameofproperty");
System.out.println(prop);
No need to develop something custom.
If you follow Spring architecture you can use this class:
org.springframework.core.io.support.PropertiesLoaderUtils

Load properties file in Servlet/JSP [duplicate]

This question already has answers here:
Where to place and how to read configuration resource files in servlet based application?
(6 answers)
Closed 6 years ago.
I've created a jar from my Java project and wanted to use the same jar in a JSP Servlet Project. I'm trying to load a property file let say sample.properties from my JSP Servlet Project kept in WEB/properties/sample.properties which should be read by a class in the jar.I'm using the following code wriiten in a class of jar to access it.
Properties prop=new Properties();
prop.load(/WEB-INF/properties/sample.properties);
But each time I'm getting fileNotFound exception.
Please suggest me the solution.
Here is the structure
WEB-INF
|
lib
|
myproject.jar
|
myclass (This class needs to read sample.properties)
|
properties
|sample.properties
The /WEB-INF folder is not part of the classpath. So any answer here which is thoughtless suggesting ClassLoader#getResourceAsStream() will never work. It would only work if the properties file is placed in /WEB-INF/classes which is indeed part of the classpath (in an IDE like Eclipse, just placing it in Java source folder root ought to be sufficient).
Provided that the properties file is really there where you'd like to keep it, then you should be getting it as web content resource by ServletContext#getResourceAsStream() instead.
Assuming that you're inside a HttpServlet, this should do:
properties.load(getServletContext().getResourceAsStream("/WEB-INF/properties/sample.properties"));
(the getServletContext() is inherited from the servlet superclass, you don't need to implement it yourself; so the code is as-is)
But if the class is by itself not a HttpServlet at all, then you'd really need to move the properties file into the classpath.
See also:
Where to place and how to read configuration resource files in servlet based application?
Try to put sample.properties under src folder, and then
Properties prop = new Properties();
prop.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("myprop.properties"));
Move your properties files under WEB-INF/classes. Then load it as following:
prop.load(getClass().getResourceAsStream("sample.properties"));
You can put it into sub-directory under classes as well. In this case change the call to getResourceAsStream() accordingly.
To be safer in multi-classloader system you can use Thread.getContextClassLoader().getResourceAsStream() instead.
To make the properties file to arrive to classes folder of your war file you have to put it under resources folder in your project (if you are using maven) or just under src folder if you do not use maven-like directory structure.
Try this,
InputStream inStream = Thread.currentThread().getContextClassLoader()
.getResourceAsStream("/WEB-INF/properties/sample.properties");
Then, load(InputStream) it into a Properties object:
Properties props = new Properties();
props.load(inStream);
it may not work If you are try to load the properties from jsp/servlet. Write a utility class to read properties and package along with jar file. copy the properties file into same package as of utility.
Class Utility{
Properties properties=null;
public void load() throws IOException{
properties.load(getClass().getResourceAsStream("sample.properties"));
}
public Object get(String key) throws IOException{
if (properties==null){
load();
}
return properties.get(key);
}
}
Now use this utility class from servlet to read the property values. May be you can define the class as singleton for better practice
Cheers
Satheesh

How to Specify Path for Properties file

I am using Config. properties file for passing parameters to my methods Now i am loading file from
Properties Config= new Properties();
Config.load(new FileInputStream("C:\\Config. properties "));
As i don't want to keep it hard coded how can i set it with package level. or within application.
Thanks in Advance.
Make use of ResourceBundle Class. You just need to specify the properties file name. It will take the file from any path,provided the path should be in the classpath.
Example:
// abc.properties is the properties file,which is placed in the class path.You just need to
// specify its name and the properties file gets loaded.
ResourceBundle s=ResourceBundle.getBundle("abc");
s.getString("key"); //any key from properties file...
I was also just going to suggest that but you can also pass in the full path to the config file via a command line argument for example:
java YourApp -config C:\\config.properties
A properties file packaged with the application should not be loaded using the file system, but using the class loader. Indeed, the properties file, once the application is packaged, will be embedded inside a jar file, with the .class files.
If the config.properties file is in the package com.foo.bar, then you should load it using
InputStream in = SomeClass.class.getResourceAsStream("/com/foo/bar/config.properties");
Or with
InputStream in = SomeClass.class.getClassLoader().getResourceAsStream("com/foo/bar/config.properties");
You may also load it with a relative path. If SomeClass is also in the package com.foo.bar, then you may load it with.
InputStream in = SomeClass.class.getResourceAsStream("config.properties");
Note that Java variables should always start with a lowercase letter: config and not Config.
If it's just the path you're worried about then you can use a relative path:
Config.load(new FileInputStream("Config.properties"));
This will look in the current working directory. The upsdie: dead simple. The downside: it's not that robust. If you start your application from somewhere else without changing the working directory before, the file won't be found.
Put the config file in the classpath (where your .class files are), and access it using
getClass().getClassLoader().getResourceAsStream(_path_to_config_file);
There are two ways to get the path of the config files at runtime.
a) Getting it from database.
b) Getting it from custom properties of JVM configured at server level
Best process is "b" , you can change the properties of JVM at any time if path is changed and just restart the server.

Categories