I am creating a java application in Eclipse Helios.I have created a class in which i need to have data from the .properties file which i added in the folder containing the solution .Through this code i can accesss the value from the .properties file when i try to run it in Eclipse.
The code is:
Properties prop = new Properties();
prop.load(new FileInputStream("config.properties"));
_url = prop.getProperty("url");
Through this code i am getting the correct value of the Url .
After this i have created an executable jar of my project .When i tried to execute the jar using command line then i got a FileNotFoundException ,means the jar is not able to locate the .properties file.
I have kept the .properties file in the same folder as of Jar file as we have the provision of editing the .properties file.
Since i started working in Java for only 4 days ,i am unable to figure out about where to place the .properties file and how to access it.
Please help.
For jar try to use:
prop.load(getClass().getClassLoader().getResourceAsStream("config.properties"));
if you call from static context use:
prop.load(YourClassName.class.getClassLoader().getResourceAsStream("config.properties"));
You need to ask the class loader to get a resource.
See http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/ClassLoader.html
Properties prop = new Properties();
prop.load(classLoader.getResourceAsStream("config.properties"));
_url = prop.getProperty("url");
Related
I have below piece of code in my Utility class which is in a .jar file. When I run the code in a local system it works fine, but when I run the JAR file using command, java -jar dbConnection.jar, it is unable to recognize jdbcDetails.properties file.
Properties prop = new Properties();
prop.load(Utility.class.getResourceAsStream("jdbcDetails.properties"));
Extract (unzip) the dbConnection.jar File and make sure 'jdbcDetails.properties' is present
Make sure 'jdbcDetails.properties' is located next to 'Utility.class'
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 have the following project structure:
ProjectName/src/java/com/main/Main.java
And I have a properties file in the following folder:
ProjectName/config/settings.properties
Now I tried to load the properties file in the Main.java with:
InputStream input = Main.class.getResourceAsStream("/config/settings.properties");
Properties prop = System.getProperties();
prop.load(input);
But this does not work. How is it the right way to do it?
Edit: I got the following error:
Exception in thread "main" java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Unknown Source)
at java.util.Properties.load0(Unknown Source)
at java.util.Properties.load(Unknown Source)
Edit2: Now it works with eclipse. I did the following (adapted from getResourceAsStream() is returning null. Properties file is not loading)
1) This directory [config] must be put into the "build path". Right-click the directory in the Package Explorer or Project Explorer view, select "Build Path", then "Use as Source Folder". Note: This build path will be the class path for the project, when you run it.
2) As the config directory now is part of your class path and contains your properties file, you can simply load it with InputStream input = Server.class.getClassLoader().getResourceAsStream("settings.properties");
This works well for eclipse but not yet for a jar. I think I also have to add the build path somehow to the ant script.
How can I do that?
Edit3: If I take the settings.properties out of the config folder in the jar, then it works. Why? I want it in the config folder in the jar too.
Use following :
InputStream input = Main.class.getResourceAsStream("../../../../config/settings.properties");
Properties prop = System.getProperties();
prop.load(input);
By adding ../ I am getting 1 step backward in your filesystem. In your code you were using /config....., which means C:/config..... (if it is in C drive).
The following will also work in your case. (but not in zipped file)
InputStream input = new FileInputStream("config/settings.properties");
Properties prop = System.getProperties();
prop.load(input);
getResourceAsStream find files relatively compiled (class) files on in jar file. Example:
Your compiled files are in bin directory than you should put properties file to bin/config/settings.properties for give access to it.
You are using maven. So follow the maven structure and keep your resources like prop, xml etc files in resource folder and then call.
So folder structure would be like this
src/main/resource|
|
config|
|
settings.properties
InputStream input = Main.class.getClass().getResourceAsStream("/config/settings.properties");
Three things
You need add the settings.properties to a jar (can be the same jar as your Main)
Your classpath should include this jar
You need to use the context Class Loader to load the properties file in you Main.
Classloader cl = Thread.currentThread().getContextClassLoader();
cl.getResourceAsStream("settings.properties");
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