Custom config file with eclipse microprofile-config - java

I want to select .properties file at runtime. Is it possible with microprofile-config? So, instead of using META-INF/microprofile-config.properties I would like to use an external file:
java -jar mymicroprofileapp.jar -s my.properties
I know, it is possible to write custom config source, but I wonder if there is a built-in option to define the config file and so avoiding repeating the code in each project

The web site tells us the three options:
System.getProperties()
System.getenv()
META-INF/microprofile-config.properties.
I addition to these you can register ConfigSources.
There is no build-in option for what you want.

Related

Custom application properties in spring boot using IntelliJ

I'm trying to use application properties, other than application.properties, say application_local.properties within resources directory.
So that I can have 2 properties files, one for local and other the server.
As mentioned in many blogs, I should use below command:
spring-boot:run -Dspring.config.location=/Users/myuser/work/MyProject/my-app/src/main/resources/application_local.properties
But this is not working, it is still fetching values from application.properties.
What am I missing, please suggest?
Thanks
1- Follow the naming convention application-{profile}.properties
application-local.properties
2-set profile
-Dspring.profiles.active=local
Briefly, you can use these two links:
How to load property file based on spring profiles
spring-profiles
-Dspring.profile.location takes directory as input. The purpose of this property is to specify additional directory location to keep your property files.
You are using property file name in your command.
Refer to detailed # Answer at other thread here
Instead you can use as suggested by #mehardad
The -D option will send parameters to Java virtual machine. In order to send parameters to Spring boot, the command option of '--' must be used.
Example:
Suppose, there is an option named 'spring.profiles.active' defined in the application.properties file as follows:
spring.profiles.active=dev
This option can be overwritten using command line parameter as follows:
java -jar application.jar --spring.profiles.active=prod
Use Spring profiles and choose at runtime, locally would
-Dspring.profiles.active=local
The property file should be called application-local.properties

How to run a jar file using specefic runtime parameters

I have a .jar file from a project I build, but whenever I run it, I use specific flags in Terminal (specifically -xDock). Is there a way to configure a .jar so that when I double click on the file it will run using whatever flags I use in terminal?
If you want to make it the Java way you can change the Java code to have default parameters - then you can just run it without. Or you can make it take its configuration from a config file and once you change that config file it will not require additional params.
If you have no control on the java code and you have a specific jar you want to double click then you can do it the windows way ;) Create a shortcut and in the Target: field add the jar with parameters
Or you can create a batch / sh file that runs it with specific params
If the parameters aren't dynamically changeable write them hard-coded or use this solution as default parameters when other parameters aren't passed.
Another interesting way of doing this, is by using JarSplice.
It's a tiny program that lets you add arguments into a sort of "wrapper" around the Jar. Then when you open it up, the arguments will be passed with it.

Java Util Logging Level using Classpath

is it possible to specify the logging level for java.util.logging using classpath? I don't want to use for that special file or create java class which overrides the default level.
My point is to say something like this -Djava.util.logging.level=ERROR
I can't find it in documentation, but maybe there are some tricks for that.
No, it is not possible. The Java Logging API can be configured either with a configuration class java.util.logging.config.class or with a configuration file. A default logging configuration file is located at "lib/logging.properties", inside the JRE directory.You could change this configuration file (which is not the best idea as it will be used for all the programs running in this JRE) or
you could set a separate configuration file for your application and set JVM property java.util.logging.config.file to point to this file
-Djava.util.logging.config.file=/tmp/logging.properties
See this tutorial for more details on Java Logging configuration.

How to configure logging when running a JAR?

I am new to Java logging API and need some help with this problem:
While creating the application, my config file was stored in the project root folder, so I used -Djava.util.logging.config.file=logging.properties switch to run the program.
But then I exported the executable JAR.
How to configure the logging now? It doesn't work, when I specify the path to config file with the -D switch.
You can't specify JVM arguments into the MANIFEST.MF file so you have to specify the logging properties at the command line or with a shortcut :
java -Djava.util.logging.config.file=logging.properties -jar yourjar.jar
Otherwise you could package a properties file(logging.properties in your case) in the JAR, read that at
startup and put those settings into the system properties.
The javadoc says:
In addition, the LogManager uses two optional system properties that
allow more control over reading the initial configuration:
"java.util.logging.config.class"
"java.util.logging.config.file"
These two properties may be set via the Preferences API, or as command
line property definitions to the "java" command, or as system property
definitions passed to JNI_CreateJavaVM.
If the "java.util.logging.config.class" property is set, then the
property value is treated as a class name. The given class will be
loaded, an object will be instantiated, and that object's constructor
is responsible for reading in the initial configuration. (That object
may use other system properties to control its configuration.) The
alternate configuration class can use readConfiguration(InputStream)
to define properties in the LogManager.
So, either use the java.util.logging.config.file system property, and store the config file out of the jar file (which is probably a good idea if you want to be able to customize the logging properties in order to debug or analyze some weird behavior), or store the config file wherever you want (in the jar file, for example), and use the java.util.logging.config.class system property to load and instantiate a class that will read the file in the jar file (Using Class.getResourceAsStream()).
I know it's little late to answer this question but I ran into this issue few days back with the runnable jar and I solved it like this:
java -Djava.util.logging.config.file=logging.properties -cp test.jar com.sample.test.Main
where test.jar is the name of your jar and com.sample.test.Main is the fully qualified name of your main class.

log4j picking up wrong properties file

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.

Categories