Please tell me where exactly do i put my .properties file in my eclipse project. Do I make a separate folder for it ?
I want to put it in such a way that I will be able to distribute my project easily in JAR form.
Thanks.
EDIT:
I want to put the properties file in such a way that it can be easily edited later, on any OS.
The approach I use in order to be able to easily change configuration without redeployment.
One version of the property file (with the defaults) in the root of your project, I always let it in the application package (foo.bar.myapp). I load the default one with getResourceAsStream("/foo/bar/myapp/config.properties") or like in the sample relative to the class package.
And additionally I ready a system property:
String configFileLocation = System.getProperty("config");
And just override the default with the properties read from the config file passed as property.
For instance:
Properties props = new Properties();
props.load(Main.class.getResourceAsStream("mydefault.properties"));
System.out.println("default loaded: " + props);
String configFile = System.getProperty("config");
if (configFile != null) {
props.load(new FileInputStream(configFile));
System.out.println("custom config loaded from " + configFile);
}
System.out.println("custom override: " + props);
This would load first your resource stored under your project in foo.bar package named mydefault.properties, and after it if system property config is configured it will load override the loaded properties with the one the the referred path.
The system property can be set using -D parameter, in this case would be something like: java -Dconfig=/home/user/custom.properties foo.bar.Main. Or if you are in a web application (Tomcat for instance) you can set this property using CATALINA_OPTS.
src/main/resource would be an ideal choice.
Related
I want to set the log4j configuration file path/other folder paths that can be used across other class files, without hard-coding the folder path.
Rightnow, I have set the variables as Environment variable. But It can only be modified if I launch Eclipse. How do I set this variable in such away that anyone (doesn't want to launch Eclipse) can modify it, from outside. Also, it will be used in test configurations. So it's better to not hard-code it and have all the file paths etc. easy to refactor.
final static String log4jpath = System.getenv("LOG4J_PATH");
Paraphrasing a comment:
[How to get value from] outside of the Java program such as a separate file, that contains all other filepaths?
That is called a configuration file.
It is often a properties file, similar to a Log4j configuration file, but it can be any types of file, e.g. XML, JSON, YAML, ...
To identify a single such file, you can use:
An environment variable (like you are right now)
A system property (more common)
A specifically named file in the current directory
...
The entries in that file will identify all the values you really want.
For example, Spring, which is a populate Java framework, will look for configuration values in many places. See 24. Externalized Configuration for full detail, but here is a summary of the most common ones:
Command line arguments, e.g. java -jar MyApp.jar --foo=bar
Java System properties, e.g. set using -Dfoo=bar on the command-line
OS environment variables, e.g. SET foo=bar (Windows) or export foo=bar (Linux)
Application properties outside of your packaged jar, i.e. relative to current directory.
Name and location can be overridden on command-line.
config/application.properties
config/application.yaml
application.properties
application.yaml
Soni, If you want to put the log4j configuration file in one place so that everybody can access. Follow the steps.
Create a project with some name and inside src/main/resources folder keep the log4j configuration file.
Create a jar file which must contain this log4j configuration file.
Use this created jar file wherever it is required. Log4j will automatically use the configuration for desired logging. If you want, you can distribute this jar file to anybody who wants to use it.
The above option is if you do not want to change the configuration file.
Now if there is a situation where someone wants to modify the configuration file.
In this case, simply put the configuration in any project classpath, means inside resource folder. As long as log4j jar files are there in the classpath and configuration files. It will log everything.
However, if you want, you can extend the functionality of Log4j by passing configuration as an object. You can refer below the link to access pro grammatically.
https://howtodoinjava.com/log4j/how-to-programmatically-configure-appenders-in-log4j/
I have added all file and folder paths inside the properties file (example config.properties) and then used it inside the testsetup method by InputStream input = new FileInputStream("Path to//config.properties");
Properties prop = new Properties();
prop.load(input);
System.setProperty("log4j2.configurationFile", prop.getProperty("log4j.path"));
this way, all files/folder paths can be modifies from outside and there's no need to set environment variable from inside the project.
I have a properties file in one of the .jar of my maven dependencies. I would like to override the values in my application so I created a file with the same name and the same package, but the values from the jar file are still being used. If I delete the properties file from the jar, the values of the file in my application are used. How can I always use the properties from my application instead of the .jar ?
As long as code takes your property file from class path it depends how your class path configured.
If you externalized your file out of any jar files - Try to put path to directory where your actual file located upfront of any other jar files in your java command -cp parameter.
If you keep your file inside your own jar file, in classpath - your jar file must be before that dependency jar file with default properties file.
Still those are not good solutions (sometime it is hard to control which path JVM will use first).
So, try to find documentation about your dependency jar - it may have a property to point from where and which properties file to use.
You can use Maven Resource Plugin and parametrize your configuration file so you can pass the parameters as arguments through command line
Use the properties resource in the jar as template, initial file for the properties file you will use:
Path propertiesFile = Paths.get(System.getProperty("user.home"),
".myapp/config.properties");
Files.createDirectories(propertiesFile.getParent());
if (!Files.exists(propertiesFile)) {
Files.copy(getResourceAsStrem("/config.properties"), propertiesFile);
}
Properties props = new Properties();
props.load(new FileInputStream(propertiesFile.toString());
How about this ?
rename the properties file with overridden values,
like this _override.properties (if the actual file is
called original.properties.
Now, in your code, read a system property, called 'toOverrideProps', if true, to load the overridden properties file
when running your program, you can set this property using the -Dprop=value method
This way, you have a choice on startup, to use the actual properties file or the overridden one, without conflict.
My Java EE application includes many sub-projects, which should use a single one configuration file to connect to the database. I intend to write a Java class and make it an independent jar to read the database connection parameters from datasource.xml, which will be put on the path of the independent jar.
The questions I want to ask:
How to dynamic get the absolute path of the datasource.xml?
Can the solution of the first question work in all operating systems like UNIX, etc?
The first subject you have to deal with is where you store that file.
According to your question, you are going to be storing the file somewhere on the file system, externally to the actual application package. Therefore, you absolutely must know where the file is really located on the file system in order to access it; you can't conclude it in advance, unless you use environment variables that will instruct your code where the file is located.
A better approach is to package your XML file with the JAR. Then, you need not worry about absolute paths anymore. Simply use Thread.currentThread().getContextClassLoader().getResource(), providing the package-style path to the resource and you'll get a reference to it wherever it may be found.
If you can't package your file along with the JAR, you might be able to store it in a directory on the file system and add that directory to your server's classpath lookup sequence; some application servers support that. Then, you can still use the classloader to look up the resource, without requiring to know its absolute location.
What about this? Converting a relative path to absolute during runtime.. I guess that should work on all enviroments...
File a = new File("/some/abs/path");
File parentFolder = new File(a.getParent());
File b = new File(parentFolder, "../some/relative/path");
String absolute = b.getCanonicalPath(); // may throw IOException
regards
You can make use of Environment Variables
String path = System.getEnv("MYVARIABLE");
File datasource = path + "/datasource.xml";
Or read in the location from a properties file available in a constant location
Properties props = new Properties();
try {
props.load("MyFixed.properties");
} catch(Exception e) {
e.printStackTrace(); // Can do better
}
String path = props.getProperty("datasource.path");
File datasource = path + "/datasource.xml";
Or pass in a VM argument when starting the Jar with -D
String path = System.getProperty("datasource.path");
File datasource = path + "/datasource.xml";
and call with
java -jar -Ddatasource.path=/my/path/to/datasource.xml my.jar
I would like to modify a properties file of a Portlet during runtime. The portlet is deployed in a Tomcat 7.0.23 and the properties file sits in "/WEB-INF/classes/content" from where I can access it via the code shown below. In fact, the code is executed without any exceptions but the newly added Property is not saved to the properties file.
String fileName = "MyProps.properties";
String relativePath = "/WEB-INF/classes/content/";
String fullPath = "c:/tomcat-7.0.23/webapps/my-portlet/WEB-INF/classes/content/";
try {
String path = relativePath + fileName;
InputStream in = getPortletContext().getResourceAsStream(path);
Properties props = new Properties();
props.load(in);
props.setProperty("test", "test");
File file = new File(fullPath + fileName));
FileOutputStream out = new FileOutputStream(file);
props.store(out, "");
} catch(Exception ex) { // error handling here}
After adding the new Property, I could verify with
props.list(System.out);
that it was added indeed. The context.xml file contains the following entries:
antiJARLocking="true"
antiResourceLocking="true"
Is this the right way to add/change Properties in a running Tomcat instance or should I take a different approach? If the latter, how could it be achieved best?
Many thanks for your answers!
You should definitely not rely on ever being able to change a file contained in a deployed web app. Control has been handed over to the container at that point and the file may be overwritten, or it may not even be writable. It also puts a burden on the application deployer, because now they cannot simply blow away the exploded WAR folder (if one exists) and redeploy the archive.
As an alternative to your approach, consider placing the properties file in a location external to the web app. One approach I've seen used successfully is this:
Determine a 'well-known' location where runtime property files will
be hosted, or allow the deployer to specify this location via a well
known property.
Attempt to read your property file from this location. If it does
not exist, create and initialize it from a template stored within
your application.
Save all changes made during the applications execution to this
external property file.
With this setup you never have to worry about not being able to write the file, or of it getting overwritten by the container.
What is the best way to store and load application level properties in Java.
Is there anything simulare to .net where I would just put something in the app.config
<appSettings>
<add key="emailAddress" value="me#example.com" />
</appSettings>
And then retrieve it like this:
string AdminEmail = ConfigurationManager.AppSettings["emailAddress"];
Do I have to load properties files into file streams and such? seems like unnecessary work.
I have tried this:
Properties props = new Properties();
FileInputStream fis = new FileInputStream("myProps.properties");
props.load(fis);
fis.close();
But this is give me a java.io.FileNotFoundException exception. Where does the properties file need to go to in relation to the class?
The Preferences API provides this functionality. It has many warts, but if you are looking to do this in an OS-agnostic fashion, this is the only way to accomplish this using the standard Java runtime libraries. You can of course always write your own OS-specific code to meet your needs. Yes, you can write simple code to load a properties file, but the location of that file can become a problem across multiple operating systems. I assume since you are writing a desktop app in Java, you care about OS portability. Otherwise Java might not be the best choice for a desktop app.
If you use a FileInputStream like that, your path is relative to the current directory of the OS, which usually is the startup directory.
If you instead use the Java built in resources mechanism (as described in the API, getResourceAsStream() et al), the path will be relative to the location of your class. With this approach you can also load resources from within jars and even over networks (for Applets for instance). The concept which is used is a sort of virtual filesystem, which is called the 'classpath' in Java jargon. There is a devx article covering it a litte more in detail.
In short, this sort of code works well:
Properties prop = new Properties();
//with properties in the same dir as current class
prop.load(getClass().getResourceAsStream("my.properties"));
//with properties in the root dir of your jar, or in base of classpath
prop.load(getClass().getResourceAsStream("/my.properties"));
You will need to add error handling...
Typically it will attempt to load from the application's current directory, which can vary depending on how you run it. You should be able to determine this at runtime by doing:
String currentFolder = System.getProperty("user.dir");
The file path it would be looking for in that case is relative to where you started your java application from. This is not where the main class is or the jar file but where you called Java from. If you are starting your application with a script that calls Java, then it is that directory.
Say for example, you application is bundled in a jar file 'app.jar'. Put 'myProps.properties' in the same directory and run 'java -jar app.jar' from that directory. It should find your properties file that way.
You can use Properties with a ResourceBundle. I use this in a application to store labels, buttons and messages in different languages
First you create a properties file, like test.properties. It´s a text file and inside it you put your information like this:
propertyname=value
In your case
emailAddress=me#example.com
email2=blablabla#example.com
and so on...
To get this properties in the code, create a ResourceBundle object with the name of your property file to call the properties.
ResourceBundle rb = ResourceBundle.getBundle("test");
To get an specific value from a properties file, just call the ResourceBundle
String value = rb.getString("emailAddress");
This way, the String named value contains the value of the property named "emailAddress", located in the test.properties file
String value2 = rb.getString("email2");
Likewise, the String named value2 contains the value of the property named "email2", located in the test.properties file
When you do not specify an absolute path, the one chosen is the current one.
It's not exactly what you asked, but if you want to use XML files as configuration, you could have a look at Apache Commons Configuration