How to add properties file into Java project? - java

I am using .properties file for my project and Netbeans 6.9.1 IDE. How can I add my properties file into the code?
String prop="c:\\Config.properties";
Properties Config= new Properties();
Config.load(new FileInputStream(prop));
f1=Config.getProperty("f1");
f2=Config.getProperty("f2");
How can I define my Build path instead of "c:\\Config.properties"?
Is there any way to add this file directly to the project?

You could put the config.properties into one of your packages and then define the path to your properties file relative.
ClassLoader.getResourceAsStream ("your/app/package/config.properties");

Niraj,
Do not use the hardcoded path for your properties file. Either get the file path from the project properties or using the build/runtime values.

Hope this will help: Adding properties

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.

How to use slash to access base directory in JAVA?

I have created a maven project and in that project one properties file. Now to access that properties file I have to put entire path of that file. How can I use slash to access the base directory, so that I just need to write:
properties.load(new FileReader("/testdata.properties"));<br>
instead of
properties.load(new FileReader("C:/Users/windows7/workspace/project/package/testdata.properties"));
May I suggest taking a look at System Properties home or class path resource instead of using slash?
System.getProperty("user.dir") will help you to navigate to the path of your directory. You can append to that path to reach to your file location.

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

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

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/

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