You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot Jar包部署失败求助:mvn运行正常但java -jar报错

Troubleshooting Spring Boot Jar Deployment Dependency Error

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.xml to ensure the resources plugin is correctly configured to include all files from src/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 package and 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:
    java -jar target\jarfile.jar --spring.profiles.active=dev
    
    Replace dev with your actual profile name. Alternatively, set a default profile in your application.properties with spring.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.xml and check dependencies that are critical for runtime (like database drivers, REST clients, or service libraries). Change their scope to compile (the default, so you can even omit the scope tag entirely) if they were set to provided or test.

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:
    mvn clean package spring-boot:repackage
    
    Or simply run mvn 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:20:36