Spring-boot application - External properties not loaded when jar execute with su - - java

I have an application in spring-boot 2.2.4
When I launch the application like this :
java -jar ../batch-pj-clean-nas/target/batch-pj-clean-nas-1.0.0-SNAPSHOT.jar -Dconfig.location=application.properties
Everything is ok.
When I try like that :
su - sbert -c "java -jar /home/sbert/workspace/pj/batch-pj-clean-nas/target/batch-pj-clean-nas-1.0.0-SNAPSHOT.jar -Dconfig.location=application.properties"
the properties is not loaded
Could not resolve placeholder 'nas.base.directory' in value "${nas.base.directory}"
I try with OS ubuntu, RH8
and spring-boot 2.2.4 or 2.1.3
and replace -Dconfig.location by --spring.config.location

Give full path of your application.properties file if you want to load it from outside the Jar

I finally found the reason :
I have to use :
su sbert -c
instead of
su - sbert -c

Related

Customize Spring Boot executable-jar startup script

I'm trying to run Spring Boot executable-jar built using spring-boot-maven-plugin on a Linux machine. The machine has multiple jdks installed, the one on PATH is jdk8 and changing it is unfortunately not an option. My executable-jar however needs jdk17, so when I just launch it as is I get UnsupportedClassVersionError.
I was following the official documentation and created the corresponding .conf file to override JAVA_HOME. But this does not seem to solve the issue:
[root#ios-maket updater-new]# ls
updater-new-3.0-SNAPSHOT.conf updater-new-3.0-SNAPSHOT.jar
[root#ios-maket updater-new]# cat updater-new-3.0-SNAPSHOT.conf
JAVA_HOME=/opt/jdk-17/bin/java
[root#ios-maket updater-new]# ./updater-new-3.0-SNAPSHOT.jar
Application is running as root (UID 0). This is considered insecure.
Exception in thread "main" java.lang.UnsupportedClassVersionError...
On the other hand if I run it manually everything works fine:
[root#ios-maket updater-new]# /opt/jdk-17/bin/java -jar ./updater-new-3.0-SNAPSHOT.jar
[main] INFO com.icl.ios.fias.updaternew.UpdaterNew - Starting UpdaterNew using Java 17.0.6
What am I doing wrong?
Setting JAVA_HOME is not enough, you also need to set PATH to point to JAVA_HOME/bin.
JAVA_HOME=/opt/jdk-17
PATH=${JAVA_HOME}/bin:$PATH
java -jar updater-new-3.0-SNAPSHOT.jar
Try running the jar with -Dloader.path to specify the config manually.
java -Dloader.path=./updater-new-3.0-SNAPSHOT.conf -jar ./updater-new-3.0-SNAPSHOT.jar
If this still does not work, then probably there is an issue with your config file, but from what I can see, your config file looks okay, unless the java path is incorrect.

How to start executable spring boot jar file with external *.ampl on Linux?

I have spring boot application with executable jar-file
So, I tried to start to from cmd in windows by command
java -jar -Dspring.config.location="application.yaml" MyService.jar
and it sucessfully started
(I try to use external application.yaml to start my application)
In my application all properties.yaml like datebase mocked like this
url: ${DATA_BASE_URL}
and external yaml contains all values.
But, when I try to start application using this command on centOS (or other linux), there is fail when try to start, spring boot used ${DATA_BASE_URL} instead external application.yaml values.
How to fix it?
On linux I used
nohup java -jar -Dspring.config.location="application.yaml" MyService.jar
According to the official documentation https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config-application-property-files
you should use double dashes
nohup java -jar --spring.config.location="application.yaml" MyService.jar

How to run Spring Boot with external configuration in Windows CMD?

I have a Spring Boot application and I'm runnig it java -jar command on CMD. But I need to use external config file that is application.properties in C:\ folder. To do this I'm running the application with this command:
java -jar app.jar --spring.config.name=application --spring.config.location=file:///C:/
But command doesn't cares the spring.config.location property and application.properties file can't be found. Because of this the application not working.
How can I run the app with spring.config.name and spring.config.location parameters in Windows CMD?
On Linux you should use file://sample-app/application.yml
On Windows you should use file:///C:/sample-app/application.yml
You try placing the jar and application.properties files in same folder and run java -jar yourapplication.jar
See if it works.

Sprin Boot apps no log dir created by running it throgh a remote ssh command

I try to deploy the sprin boot jar file into a remote Linux server and run it throw java -jar command.
The command was much like below:
ssh root#xxx.com:/myjdk_path/bin/java -jar /tmp/target.jar
The problem is that there was no any log dir been created while i running the command.
The sprin boot app was created by the spring.io site,it's a normal app then i put one single line "logging.file=./log/my.log" into application.properties.
But it works(the log dir can be created) when i just logged into the target server and type the command in the ssh console.
The sprin boot version is 2.05 and i've test version 1.5.16 still the same.
And i aslo test it in both Ubuntu and Centos servers,nothing haapens too.
Really appreciate if you can give a clue on this!
Thanks all you guys , it turns out my fault, the reason is that I am using a relative log path ,and I run that java -jar command out side the shell directory.
old command:ssh root#xxx.com:/myjdk_path/bin/java -jar /tmp/target.jar
new command:ssh root#xxx.com:cd /tmp; /myjdk_path/bin/java -jar target.jar
the log dir was been created for the old command ,but it in the user home path(for this command is the ROOT user),but not in the /tmp path.
so my stupid check steps missing that point,and thought there is no log dir been created!

How do I install Spring Boot CLI in Ubuntu?

I am trying to install Spring Boot CLI in Ubuntu. I am new to spring boot and was trying to learn it. I have already installed Gradle and groovy but did not find anything online to install Spring boot CLI.
please follow the code
sudo apt install unzip zip
curl -s https://get.sdkman.io | bash
source "/home/username/.sdkman/bin/sdkman-init.sh"=
sdk install springboot
spring version
The Spring boot CLI install guide link
Spring boot CLI
You can read the docs for installation steps:
Manual installation:
Download the Spring CLI distribution: you can find the latest version link here.
Extract the downloaded zip file to any path of your choice.
Prerequisite: Make sure you have Java JDK v1.8 or above (check INSTALL.txt file from extracted archive in Step-1). You can check your JDK version using command: java --version.
Add the spring CLI script's path to $PATH environment variable:
For example, you can add below lines to your .bashrc file in *nix systems:
export SPRING_HOME=/path/to/your/extracted-spring-cli/spring-2.4.0
export PATH=$SPRING_HOME/bin:$PATH
After saving the .bashrc file, run $ source .bashrc to reload the environment variables you just added.
(optional; and not for Windows user) Shell auto-completion scripts are provided for BASH and ZSH with the extracted archive in Step-1. Add symlinks to the appropriate
location for your environment. For example, something like:
ln -s /path/to/your/extracted-spring-cli/spring-2.4.0/shell-completion/bash/spring /etc/bash_completion.d/spring
ln -s /path/to/your/extracted-spring-cli/spring-2.4.0/shell-completion/zsh/_spring /usr/local/share/zsh/site-functions/_spring
To check if you are using BASH or ZSH, run the command: $ echo $SHELL.
DONE: To test if you have successfully installed the CLI you can run the following command: spring --version.
Here is detailed instruction on how to do it, much more useful than official one, at least for manual installation. One more thing - to make path change permanent you might need to logout - login, as "source /etc/profile" will be effective only for currently opened terminal.
Use this link to get a detailed instruction on how to download the Spring framework successfully, It really helped me a lot after long time of making research.
https://www.decodejava.com/download-and-install-spring-framework.htm

Categories