how to load Yaml file using VM argument? - java

Is there any way we can load .yml file using VM argument, like we load .properties file for log4j using -Dlog4j.configuration?
I looked for this but nothing useful found. All solutions are for Spring Boot. But I need to load yaml (from some directory which is not fixed) in an executable jar.

Sure. You can get any application parameters with
System.getProperty("my.property");
So if you start your application with
-Dmy.property=path/to/yaml/file
, in your code you can retrieve the value path/to/yaml/file as String and then you can load the file as YAML (for example, with SnakeYAML). The general case of loading YAML from a file is covered in SnakeYaml's documentation.

Related

How can I use a property defined in a ".properties" file in my "log4j2.json"?

I have an application built using the Spring Framework and written in Java.
I am using the 'log4j' library for logging and have already defined a property, fr.xyz.pathLog = mypath, in my 'dev.properties' file that specifies the file path for my log files.
I would like to use this property in my 'log4j2.json' configuration file to specify the file path for my log files.
However, when I try to use the property in the 'log4j2.json', it does not seem to be recognized.
Is there a way to use properties defined in my 'dev.properties' file in my 'log4j2.json' configuration file ?
I tried using the '${fr.xyz.path}' syntax in the 'log4j2.json' to specify the file path for my log files, but the property was not recognized when I ran the application.
I expected the property to be recognized and used in the configuration, but this did not happen.

Load external file properties with Spring Boot

I’m executing a jar and passing a properties file in args
java - jar file.jar props.properties
To read the properties file I’m using java.util.Properties.load. I want to know if there is a better way to do this with spring boot because properties file can exist in different locations or with different names but always the same properties.
I tried with PropertySource but I couldn’t find a way to set properties file dynamically
You can use:
java -jar app.jar --spring.config.location=file:///Users/home/config/jdbc.properties
More information here: https://www.baeldung.com/spring-properties-file-outside-jar

Read file from dynamic file location using talend

I am using Talend Studio for Data Integration v5.3.1.
In that I created a Job for fileDelimited. I have uploaded a CSV file and it is reading the file.
I exported the Job as a Zip file, extracted it, and I run the sh file in Terminal. And it was reading the file and displaying it in the console.
Now I want to read a different file in some other file location. is it possible to read the different file by running the same shell script? If so, where I have to change?
you can do it using context variables, and context load.
create configuration file which will have all the required input location path and other details.
you have different files on different location and you just wanted upload files without file parsing right? if so then
first create configuration file with two parameters.
FilePath|FileName
\\Folderone\|File1.txt
\\Foldertwo\|File2.txt
create two context variables in context named as FilePath & FileName
and then used tContextLoad to load above configuration details to context variable
used these variables to provide file and path at runtime from configuration.
See my answer here : https://stackoverflow.com/a/17565718/921244 to have guidance on how to open the Demo project.
There you will find a demo job for loading Context variables.
If you want an online example, take a look at the official documentation : http://www.talendforge.org/tutorials/tutorial.php?language=english&idTuto=34

netbeans and hibernate configuration

How can I load the configuration information for hibernate dynamically from a config file. Netbeans currently hard codes that information into an xml file that is then compiled into the jar. I'm a newbie to Java/Netbeans coming from PHP land and am use to a central bootstrap that pulls from a .ini or something similar, but netbeans tends to hardcode this information upon generation of the models,etc in an xml file that is then compiled in the jar. I'm looking for conventional methods of setting up configuration for various client machines using various database configurations. I don't want to have to compile the app on each machine it must be installed on.
The configuration file is read using the Configuration class. By default, it uses the hibernate.cfg.xml file found in the classpath, but you can use the configure method taking a file as parameter, and store the config file on the file system rather than in the jar.
You can also put the static mapping, which never changes between configs, in a file inside the jar, and put the varying config inside an external file. Look at the javadoc for Configuration to know how to add resources and config files to the configuration.

Read system properties from file specified as java executable argument

By launching java with the -D option you can set System.properties.
Is there a way to specify a properties file as an option for java, which in turn read them as System.properties?
E.g.
java -Dfile ./alotof.properties
I'm building a webapp deployed in JBoss. The jboss xml configuration files accepts system properties as inline config {my.property}, which reads from the command line argument but this gets unruly as the number of properties grow.
You can read the properties file in bash (in run.sh file), parse properties and create the proper config line for JVM.
Here you can find 2 articles that can help you:
Reading Java-style Properties Files with Shell (permalink)
Reading java.properties file from bash
I don't know about giving a direct property file as an argument, but instead you can have startup class which loads at the bootstrap and overrides whatever property you want override from your property file using System.setProperty() method.
Java can't read system properties from a file, but JBoss can - use the SystemPropertiesService, configured through properties-service.xml in the deploy directory.

Categories