How do I tell Spring Boot which main class to use for the executable jar?
Add your start class in your pom:
<properties>
<!-- The main class to start by executing java -jar -->
<start-class>com.mycorp.starter.HelloWorldApplication</start-class>
</properties>
or
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.mycorp.starter.HelloWorldApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
how does maven-spring-boot-plugin determine the main class?
Yes, compile everything in the "ClassPath". In this case is the
src/main/java
you mentioned.It will look for the main method which is in a class with
@SpringBootApplication
annotation.
Error: Could not find or load main class Maven Spring Boot Application - Executable JAR
When it comes to spring boot application you can't create a regular jar file, because spring boot application jar is not a jar at all. It doesn't have an internal directory layout of jar (all dependencies are in BOOT-INF/lib for example) and there are some other subtle differences.
So I believe the issue is with maven here.
Instead of trying to create jar "by youself" and add all the dependencies there you should use spring boot maven plugin:
It "knows" how to create a spring boot jar properly.
By default it will also resolve your "main" file (the one with method main
and @SpringBootApplication
annotation) however you can specify it in plugin configuration to be on the safe side.
Then you'll be able to the project with :
java -jar <Name_of_your_jar>
Launching main class inside a nested executable jar created with spring-boot-maven-plugin
Finally after a long research and with the help of my team lead, I am able to figure out the solution. It is actually an issue of executable jar dependency created by spring-boot-maven plugin. My starter class was not able to locate the Foo.java or Bar.java in dependency list because those jars are also executable jars. Finally I have added these lines in my foo-project and bar-project POM file:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<phase>none</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
This makes the foo-project and the bar-project jars general ones and everything works as expected.
Hope this helps someone to work with spring-boot-maven plugin.
How can I tell Spring Boot to place some of the classes at the root of the jar instead of BOOT-INF?
This issue is a result of BOOT-INF fat jar structure introduced by Spring Boot 1.4.
There is currently no straightforward solution, and it appears some of the Spring Boot maintainers do not agree there is a problem, so it could be a long time before the situation changes:
Issue #6626: Make it easier to package certain content in the root of a fat jar
Issue #12659: Starting executable war with -Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager produces a ClassNotFoundException
WORKAROUND #1
I had to do two things to get my application working again with a custom log handler. 1) use Maven Shade to package up the log handler with all its dependencies, and 2) launch the app with using the PropertiesLauncher
class in the command line instead of using java -jar
:
java -cp executable.jar:logger-shaded.jar -Dloader.main=mypackage.myapp org.springframework.boot.loader.PropertiesLauncher
The executable.jar
, logger-shaded.jar
, and mypackage.myapp
are placeholders specific to my project, so adjust accordingly.
WORKAROUND #2
If the handler is loaded from code in a config class or from main()
instead of being specified in the file loaded via java.util.logging.config.file
, as discussed in the comments to the answer in this other question, then everything works as expected. I actually prefer this over Workaround #1 as it results in a smaller deployment, but it does require writing a few more lines of code.
Spring Boot Program cannot find main class
Main class is configurable in pom.xml
<properties>
<start-class>com.bt.collab.alu.api.webapp.Application</start-class>
</properties>
Related Topics
Scheduledexecutorservice Exception Handling
In Which Thread Do Completablefuture's Completion Handlers Execute
Java String.Indexof and Empty Strings
Get All Variable Names in a Class
Multiple Inheritance for an Anonymous Class
Allowing the "Enter" Key to Press the Submit Button, as Opposed to Only Using Mouseclick
Noclassdeffounderror While Trying to Run My Jar with Java.Exe -Jar...What's Wrong
Can an Interface Extend Multiple Interfaces in Java
Dynamically Find the Class That Represents a Primitive Java Type
Try with Resources Introduce Unreachable Bytecode
Animate JPAnel (Slide In) with Timer
Rounding Up a Number to Nearest Multiple of 5
Java: String Concat VS Stringbuilder - Optimised, So What Should I Do