IntelliJ IDEA打包Spring Boot Jar异常:无法找到或加载主类
Hey there, I’ve run into this exact issue a handful of times when working with Spring Boot, so let’s break down the most likely fixes to get your JAR running properly:
1. Verify Your Build Tool Configuration (Maven/Gradle)
The biggest culprit here is usually missing or misconfigured Spring Boot packaging plugin. Spring Boot requires its own plugin to create an executable "fat jar" that includes all dependencies and sets up the correct main class entry.
For Maven:
Make sure your pom.xml includes the Spring Boot Maven plugin with proper main class configuration (replace com.yourpackage.Main with your actual full class path):
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.yourpackage.Main</mainClass> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
For Gradle:
Check your build.gradle has the bootJar configuration set:
plugins { id 'org.springframework.boot' version 'your-spring-boot-version' id 'io.spring.dependency-management' version 'your-dependency-management-version' id 'java' } bootJar { mainClass = 'com.yourpackage.Main' }
2. Confirm You’re Building an Executable Fat Jar
Not all JARs are created equal—Spring Boot’s executable JAR has a specific structure. Run this command to inspect your JAR’s contents:
jar tf your-app.jar
Look for two key things:
BOOT-INF/classes/com/yourpackage/Main.class: This confirms your compiled main class is present in the correct location.META-INF/MANIFEST.MF: Open this file (you can usejar xf your-app.jar META-INF/MANIFEST.MFto extract it) and check for these lines:Main-Class: org.springframework.boot.loader.JarLauncher Start-Class: com.yourpackage.Main
If these lines are missing, your JAR isn’t a proper Spring Boot executable JAR—recheck your build plugin setup.
3. Use the Correct Run Command
Never try to run the Main.class file directly—Spring Boot relies on its JarLauncher to handle classpath and dependency loading. The only correct command to run your JAR is:
java -jar your-app.jar
Double-check you’re running this from the directory where your JAR is located, or use the full file path (e.g., java -jar target/your-app-0.0.1-SNAPSHOT.jar).
4. Double-Check Your Main Class Package Path
It’s easy to overlook the full package name when specifying the main class. For example, if your Main.java is in the com.myapp.demo package, the full main class name must be com.myapp.demo.Main—not just Main.
5. Clean and Rebuild to Clear Caches
Sometimes old compiled files or cached build artifacts cause issues. In IntelliJ:
- Go to
Build > Clean Projectto wipe old build files. - Then run your build tool’s package command (for Maven:
mvn package; for Gradle:gradle bootJar). - You can also manually delete the
target(Maven) orbuild(Gradle) directory before rebuilding to ensure a fresh start.
Give these steps a try—9 times out of 10, the issue is either a misconfigured build plugin or a missing package name in the main class setup.
内容的提问来源于stack exchange,提问作者user12860315

