spring boot System.getProperty("java.class.path") doesn't include lib/ jars - spring-boot

I've got a spring boot app and I'm building a myApp.jar using the spring-boot antlib. When I jar -tf myApp.jar I see that I have a jar called lib/foo.jar. Yet when I print out System.getProperty("java.class.path") I don't see that jar file on the classpath. I also get a ClassNotFound exception from URLClassLoader when the code attempts to use this class for the first time. I'm using the JarLauncher since that's what the antlib defaults to.
Any ideas why this jar file would not be on the classpath?

You won't see a bundled JAR in System.getProperty('java.class.path'). The class path specifies where the JVM will look in the filesystem for classes you attempt to load.
Spring Boot uses fat JARs, which are loaded in a completely different way. Refer to the Spring Boot documentation.

Related

Difference between Spring boot 2.5.0 generated *jar and *-plain.jar?

I upgraded my spring boot app to 2.5.0, then is app.jar and app-plain.jar is created by gradle.
I would like to the difference between these jars.
thanks
app.jar is the archive produced by the bootJar task. This is a Spring Boot fat jar that contains all of the module's dependencies as well as its classes and resources. It can be run using java -jar.
app-plain.jar is the archive produced by the jar task. This is a plain or standard jar file that contains only the module's classes and resources.
You can learn a bit more about this in the documentation for Spring Boot's Gradle plugin.

How to Run the spring boot jar

I have spring boot jar. It contains boot-inf folder with that folder it contains classes and lib folder. I need to run the certain class which is having main method. But I don't know how to run it. For normal jar we can use the below format to run the class from the jar.
java -cp "sample.jar;dependecy.jar" com.sample.ClassName
But in spring boot jar what is the format to run the class. Because it contains boot-inf folder. I am using gradle script to build the project
It should be very easy, just:
java -jar sample.jar
where sample.jar is your spring boot jar.
See https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-running-your-application.html
You can try with the distribution to run the class in a jar. Build the project to create the dist. Then you can run the class as you mentioned in the question.
java -cp "sample.jar;dependecy.jar" com.sample.ClassName

my application.properties from dependency is not available in spring boot application

I have spring boot application.
In this spring boot (fat-jar) application I have classpath:config/application.properties and classpath:application-env.properties.
I am starting this application using command:
java -Dspring.profiles.active=env -jar application.jar --some-parameter=some-value
Question: Why application.properties file is not available during initialization this application from classes (beans) localized in my dependency?
This file (application.properties) is localized in classpath:application.properties of my dependency.
I am expecting some properties with some specific prefix - using annotation:
#ConfigurationProperties(prefix = "someprefix")
and again the same in other words:
Application A have classpath:config/application.properties
+ Application A have/is using dependency to some artifact D
dependency (jar) D.jar have classpath:application.properties
this application.properies is not available during initialization of bean B with annotatin #ConfigurationProperties(prefix = "someprefix")
according to 24.3 Application property files "The classpath root" should be fine - but it is not.
This is fat-jar application - in one big jar we have everything - including folder 'lib' with all dependencies.
OK, :(
solution of my issue was trivial. My application was loading different classpath:/application.properites than I thought.
I had another dependency jar introduced to my project not-directly (it was dependency for my dependency) which contains different classpath:/application.properites.
In other words everything works fine but only one 'classpath:/application.properties' is loaded even you have two in two different dependencies.
I used /actuator/env to find out which classpath:/application.dependency was loaded.

Spring Boot - will the executable jar be exploded in after deployment?

From the documentation of Spring Boot, I can see the directory of the executable jar will be the working directory when you start the Spring Boot application. I would like to understand, whether there are configurations/flags to explode/unpack the jar on deployment, so that I can find access the contents of the executable jar in the file system ?
There are not flags to explode the jar automatically but you can extract it yourself using the jar command. This reference may be helpful http://docs.spring.io/spring-boot/docs/current/reference/html/executable-jar.html in understanding the structure and what you can do.

How to put a directory first on the classpath with Spring Boot?

I am building my Spring Boot application using Maven, so I can start it with:
java -jar myjar-1.0-SNAPSHOT.jar --spring.profiles.active=prod
I want to have a directory first on the classpath that would allow me to place some files on the filesystem without having to unzip the jar to change them.
I have tried using loader.path, but it does not seem to work.
java -Dloader.path="config/*" -jar myjar-1.0-SNAPSHOT.jar --spring.profiles.active=prod
The config dir is a subdirectory of where the jar is located. I am trying to load a keystore file which is injected as a Resource in my application. There is such a file in the src/main/resources, but that only works in my IDE, not when packaged as a jar. So I want to put a file first on the classpath so that that one is found first on the classpath.
You can use loader.path but only if the Main-Class is PropertiesLauncher (so it depends how you built the JAR file). Maybe you need to re-build the JAR with packaging=ZIP in the Boot plugin (e.g. docs here)? Can you not set the path to the keystore as a "file:" URL?

Resources