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

运行Spring Boot Jar文件报错:创建'dataSource' Bean失败

Hey Roland, let's troubleshoot this HikariDataSource bean creation error you're facing when running your Spring Boot JAR via CMD. This is a super common issue with a handful of straightforward fixes—let's go through them one by one:

Common Troubleshooting Steps & Fixes

1. Verify Your Database Config is Loaded Correctly

  • First, check if your application.properties/application.yml is actually packed into the JAR. Sometimes IntelliJ misses resource files during packaging. Run this command in CMD to inspect the JAR's contents:
    jar tf your-app-name.jar
    
    Look for your config file under BOOT-INF/classes/—if it's missing, you'll need to adjust your IntelliJ build settings to include resources.
  • If the config file is present, double-check the database URL, username, and password for typos. Small mistakes like missing port numbers, extra spaces, or incorrect database names break the connection instantly.
  • If you're using Spring profiles, make sure you specify the correct one when running the JAR:
    java -jar your-app-name.jar --spring.profiles.active=your-target-profile
    

2. Fix Dependency Issues

  • Spring Boot's Hikari auto-config requires a valid database driver. For example, if you're using MySQL, confirm the mysql-connector-j (or older mysql-connector-java) dependency is included in the JAR. Check with:
    jar tf your-app-name.jar | grep mysql
    
    If it's missing, add the dependency to your pom.xml (Maven) or build.gradle (Gradle) and re-package.
  • Dependency conflicts can also cause this error. If you manually added HikariCP or a database driver with a version that clashes with Spring Boot's default, run these commands to spot conflicts:
    • Maven: mvn dependency:tree
    • Gradle: ./gradlew dependencies
      Exclude the conflicting dependencies from your build file.

3. Check Database Connectivity

  • Test if your database is reachable from the machine where you're running the JAR. Ping the database server first—if it's local, make sure the database service is started. For remote databases, confirm firewalls allow traffic on the database port (e.g., 3306 for MySQL).
  • For newer databases like MySQL 8.0+, you must specify the timezone in your JDBC URL:
    spring.datasource.url=jdbc:mysql://localhost:3306/your-db?serverTimezone=UTC
    
    Missing this often causes connection failures that trigger the DataSource bean error.

4. Fix IntelliJ Packaging Configuration

  • If you're using Maven, ensure the spring-boot-maven-plugin is properly configured in your pom.xml—this plugin builds an executable JAR with all dependencies included. Run the package goal (not the standard jar goal) to build the JAR.
  • For Gradle, use the bootJar task instead of the default jar task. This ensures all required dependencies are bundled into the executable JAR.

5. Check File/System Permissions

  • On Windows, if your JAR is stored in a protected directory (like C:\Program Files), the user running the JAR might not have permission to read the config file or access system resources. Move the JAR to a regular directory (like your Desktop) and try again.

内容的提问来源于stack exchange,提问作者Roland

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 09:19:10