I'm encountering an issue when trying to run a Spring Boot application from a jar file that includes its dependencies. Here's my setup:
I've created a new Spring Boot Gradle application and configured the jar task in my build.gradle file to include dependencies inside the generated jar file using the following snippet:
jar {
archiveFileName = "${project.name}-test.jar"
zip64 = true
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
manifest {
attributes 'Main-Class': 'com.example.demo.DemoApplication'
}
from {
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}
}
The jar file is generated successfully. However, when I try to run the generated jar using java -jar demo.jar, it starts but immediately stops. Interestingly, if I use bootJar, the application runs perfectly.
My goal is to improve startup time, and I've explored solutions like GraalVM. However, compatibility issues arise with dependencies such as Log4j, Flyway, and Liquibase, especially considering that my project has multiple modules with their own dependencies.
When I tried the same approach mentioned above with another project, I encountered different issues:
Error: Could not find or load main class mobi.foo.yeepalwallet.FooApplication
Caused by: java.lang.ClassNotFoundException: dd.ss.main.class
I attempted to utilize the Shadow library (com.github.johnrengelman.shadow), but encountered numerous errors. Despite this, I managed to execute the generated JAR, albeit with different errors.
You can generate the Spring Boot project using the following link: Spring Initializr
I'm seeking guidance on the correct approach to achieve faster application startup time while including dependencies in the jar. Any insights or suggestions would be greatly appreciated. Thank you.
Comment From: wilkinsona
Thanks for getting in touch, but it feels like this is a question that would be better suited to Stack Overflow. As mentioned in the guidelines for contributing, we prefer to use GitHub issues only for bugs and enhancements. Feel free to update this issue with a link to the re-posted question (so that other people can find it) or add some more details if you feel this is a genuine bug.
You may also want to read the documentation on efficient deployments.
Comment From: Goooler
Duplicate of #1828