Spring Boot Jar包部署失败求助:mvn运行正常但java -jar报错
Hey there! Let's figure out why your Spring Boot app works perfectly with mvn spring-boot:run but throws that frustrating UnsatisfiedDependencyException when you try to run the packaged JAR with java -jar target\jarfile.jar. This is a common issue, and here are the most likely causes and fixes:
1. Missing Configuration/Resource Files in the JAR
When you run mvn spring-boot:run, it pulls resources directly from your local src/main/resources folder. But if your Maven build isn't properly including these files in the final JAR, the app can't find critical configs (like database connections or service endpoints) when running as a standalone JAR—leading to dependency injection failures.
Fix:
- Check your
pom.xmlto ensure the resources plugin is correctly configured to include all files fromsrc/main/resources:<build> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*</include> </includes> </resource> </resources> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> - Rebuild the JAR with
mvn clean packageand try running it again.
2. Missing Active Profile Configuration
Chances are you're using a specific Spring profile (like dev or prod) when running via Maven (e.g., mvn spring-boot:run -Dspring.profiles.active=dev), but forgot to specify it when launching the JAR. Without the correct profile, the app loads default configurations that might not have the dependencies set up properly.
Fix:
- Add the profile parameter when running the JAR:
Replacejava -jar target\jarfile.jar --spring.profiles.active=devdevwith your actual profile name. Alternatively, set a default profile in yourapplication.propertieswithspring.profiles.active=your-profile.
3. Incorrect Dependency Scope
If some of your dependencies have a scope set to provided or test, Maven will include them during the spring-boot:run phase but exclude them from the final JAR. This means when you run the JAR, those dependency classes are missing, causing the dependency injection to fail.
Fix:
- Open your
pom.xmland check dependencies that are critical for runtime (like database drivers, REST clients, or service libraries). Change their scope tocompile(the default, so you can even omit thescopetag entirely) if they were set toprovidedortest.
4. Not Building an Executable JAR
If you aren't using the Spring Boot Maven Plugin correctly, you might end up with a plain JAR that doesn't include all the necessary dependencies or the Spring Boot bootstrap logic. mvn spring-boot:run handles this under the hood, but the standalone JAR won't work without it.
Fix:
- Ensure the Spring Boot Maven Plugin is present in your
pom.xml(as shown in the first fix). Then build the JAR using:
Or simply runmvn clean package spring-boot:repackagemvn clean install—the plugin will automatically repackage the JAR into an executable one.
5. Case Sensitivity Issues (Windows vs. JAR Filesystem)
Windows ignores file path case, but JAR files use a case-sensitive filesystem (like Linux). If your code references a resource with a different case than the actual file (e.g., classpath:Config/dev.properties vs. src/main/resources/config/dev.properties), mvn spring-boot:run will work fine, but the JAR will fail to find the resource.
Fix:
- Double-check all resource paths in your code and ensure they match the actual file names and directories exactly (case included).
内容的提问来源于stack exchange,提问作者machal

