I have a properties file named xyz.properties that is in the root folder of my Java Project. Now I want to change the location to some other place (say Desktop on Windows).
Is the properties file have to be on the root folder? Could my Java application read it from another location outside the project?
Any help is appreciated.
If properties file is on the classpath then it can be loaded using the generic code as mentioned here:
Properties props = new Properties();
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(propFileName);
Related
I have a file named config.properties in Eclipse with the following content:
PATH_TO_A_FILE=a.txt
PATH_TO_B_FILE=b.txt
PATH_TO_C_FILE=c.txt
In my code, I need to use these properties like this:
conf.put("PATH_TO_B_FILE", properties.getProperty("PATH_TO_B_FILE"));
which files A.txt and B.txt are in the same path of the config.properties in the workspace folder
What I should do read those paths from my config.properties file?
Also: should I add a path before b.txt?
Should I write it as /home/user/workspace/b.txt or .home.user.Dersktop.b.txt?
Suppose that you have a file with path /home/user/Desktop/my.conf that contains properties, then you can load these properties in a Properties object like this:
Properties properties = new Properties();
InputStream input = new FileInputStream("/home/user/Desktop/my.conf");
properties.load(input);
Now you can get the properties like this:
String pathToA = properies.get("PATH_TO_A_FILE");
Obviously, you shouldn't put that properties file my.conf on your desktop. Are you creating a web app? In that case, you can ship the properties file with your jar. Files can be read from a jar using an InputStream, but that's a different question ;-)
Whether or not you should add a full path in the properties file also depends on the context of your application. In a server environment, you may not have a /home/user/Desktop directory. If you want an answer to that second question, you should clarify the context of your question. For instance: is your application a desktop or server application? what is the working directory of your application?
In C#,when I want to create a configuration file, it's so easy,just right click the mouse and add a new configuration file, this file will be added into the solution and it's so easy to maintain.
But in java, I don't know what method is standard. I see some people use the properites file.If this is the most popular method, can some one tell me where to place this file? I saw some guy put it in the src folder, others put it in an external folder.
Can you tell me which is the standard? And what is the best practice to maintain a configuration.
I don't know if this is the "standard" way but I think it's the easiest. If you place your properties file in your project's root folder
- project
- config.properties
- src
- main
- ...
- test
When you create a File instance in Java and specify a relative filename, then the name is resolved against the directory that Java was launched from
e.g. if you launch java in your command prompt as follows:
cd C:\Users\Tom\example-project
java example-project
and this is your code:
File file = new File("tom.txt");
then the file variable will be resolved to the abolsute path: C:\Users\Tom\example-project\tom.txt
When you Run a project through Eclipse, Eclipse launches java from the root directory of the project, meaning that if you put your config file in the project's root folder then
File file = new File("name-of-config-file.properties");
will resolve to the correct config file on your system.
This has an added benefit if you create a runnable JAR, as you can just place your config file in the same directory as your JAR and the code will continue to work (the config file location will be resolved relative to the JAR).
If you put your config file in /src folder then you need to have separate code for when running from Eclipse and when running as a JAR
With regards to sample code:
//Read properties from disk
File propertiesFile = new File("config.properties");
FileReader reader = new FileReader(propertiesFile);
Properties props = new Properties();
props.load(reader);
//Set and get properties
props.setProperty("NewProperty", "value");
String propValue = props.getProperty("propToGet");
//Write properties to disk
FileWriter writer = new FileWriter(propertiesFile);
props.store(writer, "Added x properties");
Configuration files are used to store,read write user settings.
I think for web apps you can use web.xml.And for other you should use Properties class to read and write settings.
As for where to place it,If you dont specify path it is stored in your root folder other than that you have to provide explicit path.
I was trying to work on a java program with requires a properties file attached to the jar. Thus, i did jar the program, and unjar it to attach a properties file. I have a class, MyClass, which requires the properties file. Thus, i basically put the properties file to the same folder as MyClass.class. Then I jarred it back.
In my MyClass.java, i was trying to access the properties by:
Properties prop = new Properties();
prop.load(MyClass.class.getClassLoader().getResourceAsStream("prop.properties"));
Where prop.properties is in the same folder as my MyClass.class.
It failed to load it. Any help will be appreciated !
Thanks,
ClassLoader.getResourceAsStream() treats the path as relative to the root of the classpath, i.e. it looks for resources at the root level of each JAR or directory on the class loader.
You probably want Class.getResourceAsStream, which treats the path as being relative to the package of the class in question. You also need to make sure to close the stream once you've read it.
Properties prop = new Properties();
InputStream in = MyClass.class.getResourceAsStream("prop.properties");
try {
prop.load(in);
} finally {
in.close();
}
If MyClass is in the package com.example then this will load com/example/prop.properties from the JAR.
I have simple java project with structure:
package com.abc:
a.java
b.java
c.properties
I have database configuration parameters configured in c.properties file.
Inside a.java and b.java, I am loading properties file using:
Properties p = new Properties();
InputStream in = this.getClass().getResourceAsStream("c.properties");
p.load(in);
This works fine. But the main question is, once I prepare executable jar by exporting this code, properties file also gets packaged in jar file. If someone else wants to modify properties file for different database configuration, how can he do it?
Do I have to store properties file in some fixed location in local machine. e.g. "c:/". Then give jar along with properties file to the other person. Then he needs to copy properties file inside C:/ location?
Also one more question, how can i make this location generic for windows and linux machine?
The typical way of handling this is to load the base properties from your embedded file, and allow users of the application to specify an additional file with overrides. Some pseudocode:
Properties p = new Properties();
InputStream in = this.getClass().getResourceAsStream("c.properties");
p.load(in);
String externalFileName = System.getProperty("app.properties");
InputStream fin = new FileInputStream(new File(externalFileName));
p.load(fin);
Your program would be invoked similar to this:
java -jar app.jar -Dapp.properties="/path/to/custom/app.properties"
First keep the default properties in your properties file, which gets packed into the jar. When the application starts try reading a same named properties file from some default location in filesystem, preferrable the user's home folder which you can obtain by System.getProperty("user.home");. If the file exists at the filesystem load it, if it doesn't exist then load your packed properties file and write a copy to the filesystem.
So if your properties file name is myprops.properties, initially only your jar file will contain it. When the application starts up it will check whether /home/xyz/myprops.properties file exists. Since it doesn't, it will read the packed properties file and write a copy to /home/xyz/myprops.properties file. From next time onwards, it will read from /home/xyz/myprops.properties.
Why not pass the location of the properties file as a command line argument (following a flag)? if it's not present, then use the default one in the jar file.
You're loading the properties file from the class path. I'd suggest something like this:
Properties location
I have created a dynamic web project within Eclipse. I created a properties file inside the src directory:
<project-root>/src/props.properties
I'm starting Tomcat via Eclipse in debug mode. Now I want to read the properties file from one of my POJOs, but I get a FileNotFoundException. The current path seems to be the Eclipse path.
I had a look into the web for solution, but none of them worked for me. Maybe I did something wrong. The code is like this:
File file = new File("props.properties");
FileReader reader = new FileReader(file);
properties.load(reader);
How should I acces the properties file?
Where should it be located?
Thanks in advance.
If its a web application then the properties will get deployed to WEB-INF/classes and can be loaded using the class loader
InputStream in = {NameOfClassWhereThisisInvoked}.class.getResourceAsStream("/props.properties");
properties.load(in);
in.close();
Actully that should work regardless of whether it is a webapp or not.
I'm assuming src is defined as a source folder
There is another way of doing this as follows:
File file = new File(session.getServletContext().getRealPath("/") + "props.properties");
Instead of "props.properties" you could give any path relative to the "WebContent" folder of your eclipse web-application project.
here is the correct way to load properties file from anywhere in the classpath
private Properties getPropertiesFromClasspath(String propFileName)
throws IOException
{
Properties props = new Properties();
InputStream inputStream =
this.getClass().getClassLoader().getResourceAsStream(propFileName);
if (inputStream == null)
{
throw new FileNotFoundException("property file '" + propFileName
+ "' not found in the classpath");
}
props.load(inputStream);
return props;
}
You can create a singleton class to load properties in memory on first time access and later use them via a static method. A complete example of this is available at
http://bharatonjava.wordpress.com/2012/09/12/using-properties-file-in-java-application/
Kepp ur Properties file in src folder and the following code is enough to read the properties file
**File file = new File("./src/config.properties");
FileReader reader = new FileReader(file);
prop.load(reader);
System.out.println(prop.getProperty("directorypath"));**
Is your project set to build automatically? If so (or you're building manually), check whetherprops.properties appears in your project's output folder (in Eclipse this is usually "bin" but may be WebRoot or something else, depending on how your project is set up).
When a project builds, it should copy over config files as well as your compiled classes, JSPs etc. However, I believe that file readers, by default, use the JVM's home directory for their source. In Eclipse's case, this means that your props.properties file would have to be in the project root, i.e. not the "src" folder.