My code generates few properties file at runtime and these will be used by other portion of code.But the other portion of code expects that those properties files in the classpath.
Is there any way to generate the files in classpath at runtime.
You could simply create empty files which are set on the classpath up front and the application appends to these as necessary.
I'm wondering though, do you need to touch the hard disk at all? Could you not use a cache instead?
Related
I want to write a text file at classpath. want to make some changes in that file.
this is spring boot application and packaging it as a jar. So basically this text file is located in jar & I want to make changes to that file.
Don't know it is possible or not.
but please suggest to me how I can do that?
Files inside a jar can be read from (called resource files in general).
You cannot modify them directly though. For that, you need them to be outside the jar.
Possible duplicate of Updating resource files at runtime
If it is a .properties file though, there are ways to do it.
Following blog seems helpful (https://crunchify.com/java-properties-files-how-to-update-config-properties-file-in-java/)
I have a large number of user settings that I want to store within an XML file. If it is possible, I would like to keep that XML file within the JAR itself, but so far I have found nothing that permits me to do that (lots of searching on Google turned up nothing).
I plan to use JAXB to handle reading and writing the XML file but first I need to know if it's possible to write to the XML file within the JAR during run time.
So far getClass().getResourceAsStream() seems to be on target except it only returns an input stream to read from the file. Is it possible at all to write to a file stored as a resource within a JAR file at run time? To open that file for Output?
The Java process will lock dependencies on the classpath. You won't be able to change your JAR because the OS will tell you that it is in use.
java -cp "your_jar.jar;your_jar2.jar" YourMainClass
Only way I can think of is to load your JAR at runtime, so you can change a second jar that would be part of your application.
I (kinda) understand why you are doing it, but don't do it. Keep configuration out of your JAR.
I'm currently working on an internationalized webapplication in java, using only the standard servlet api (no frameworks). for all static text on the pages like headings, labels etc. I've been using the fmt tag library, backed by properties files in WEB-INF/classes.
the application is almost done, but the requirement that our client might like to change or update the translations later on, has suddenly been introduced.
Since the properties files are located inside the war, this is not doable without recompiling the app. so, my question is simply: is there any way of updating the properties files inside the war or maybe have the setBundle tag load the files from an external directory. or maybe a third, more clean and correct way to achieve this?
A war is just a zip file. Unzip it, change the properties files, and rezip. No need to recompile anything.
Providing a simple script to do that in a single operation should be easy. You could even use the u (update) option of jar to do it. See http://download.oracle.com/javase/tutorial/deployment/jar/update.html
Put them in an external folder and add its path to the webapp's runtime classpath. For example, /var/webapp/conf. As to adding this path to the webapp's runtime classpath, this depends on the server used. If it's for example Tomcat 6/7, then you need to add it to the shared.loader property of Tomcat/conf/catalina.properties file.
shared.loader = /var/webapp/conf
This way it's available in webapp's runtime classpath the usual way and you don't need to repackage the WAR..
There are 2 log4j.properties files in my classpath. I need both of them - One of them is required for a library that I am using and another is the one used by my code. When I run my jar file, it is able to read the properties used by the library, but it is not reading my own properties file. How can I make it read my log4j without having to use PropertytConfigurator in all my source files? Is there any way I can configure it so that it used both the properties files together?
To answer your first question, you can point it to your own file by giving it a unique name and adding the following system property when you launch your application.
-Dlog4j.configuration=path_to_my_properties_file
I don't think it is possible to use 2 different files without doing anything programatically.
Two log4j.properties files will surely create a mess (as you've experienced).
I'd suggest removing the library's version (why is it a requirement?), and combining both .properties files into one.
All logging goes into a single property file. Within that file you can differentiate between your own classes and the library's logging configuration.
Can .properties values be stored statically in compiled classes?
I am updating a value in my .properties file yet the log is still showing the old value from the original .properties file. Is this because the class file has stored the .properties values during compilation? (I do not have the original source so cannot recompile)
Properties are not set statically in compiled class. Most likely you have old properties somewhere in classpath, or, maybe, another property file with the same property. Try to run your application in verbose mode in order to get classpath and search in it for properties files loaded.