How to Run the spring boot jar - spring

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

Related

spring boot jar not picking profile specific properties outside jar

I have a spring boot jar(name : myjar) located in the directory structure
D:/hello/myjar
The fat jar contains profile specific application.properties in src/main/resources for e.g. application-local.properties
I want to override the attributes defined in the application-local.properties inside the jar with application-local.properties outside the jar
Hence I created application-local.properties and kept it in the same folder as jar i.e. D:/hello
However when I run my jar using the command :
java -jar -Dspring.profiles.active=local D:/hello/myjar.jar
it still picks the properties which is inside the jar. Am I missing something ?
Try this use -D before -jar
java -Dspring.profiles.active=local -jar D:/hello/myjar.jar
let's see https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config
I think it was more of point that from where I was running my jar from the command prompt.
If I run the jar from the folder where my jar is present, it does picks the profile specific files present outside the jar.

How to generate executable scripts using spring boot maven plugin

Using spring boot maven plugin we are able to generate executable jars. And we can execute the jar using java -jar ...
In spring boot there is another option for installation . This generates the jar which can be added in init.d.
But is it possible to generate a sh|cmd file which can be used to start|stop|restart spring boot applications?
The executable true flag to create a 'fully executable’ jar actually pre-pends a shell script into the beginning of the jar.
It works outside init.d too. Try this:
./myapp.jar start

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

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.

Spring Boot with Groovy and External Libraries

I'm working on building what is effectively a throwaway Spring Boot application. Using the CLI, I can get a basic page up and working (see https://spring.io/guides/gs/spring-boot/). What I haven't figured out how to do, however, is how I can add external dependencies (ie. third-party JAR files) to the compile or runtime classpath when I use either the "spring run" or "spring jar" commands. Note that these external dependencies are local to my computer and are not stored in an artifact repository. Is there a simple way to do this?
If the jars aren't in an artifact repository, the easiest way to add them to the classpath is to use -cp when running your app or creating its jar.
For example:
spring run -cp foo.jar app.groovy
Or:
spring jar -cp foo.jar app.jar app.groovy
In the spring jar case, anything that's added to the classpath using -cp will be packaged inside the resulting jar (app.jar in this case) ensuring that it's self-contained.
you can use groovy's #Grab notation (there is also spring grab call). E.g.
#Grab('joda-time:joda-time:2.5')
#RestController
class ThisWillActuallyRun {
#RequestMapping("/")
String home() {
return new org.joda.time.DateTime().toString()
}
}

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