java - 运行 spring mvc + spring boot gradle java 9 项目,最终出现 java.lang.NoClassDefFoundError : java/sql/SQLException

标签 java spring spring-boot gradle

我有一个非常简单的应用程序,可以完美地与 Java 1.8 一起工作,它包含一个像这样的主类

package com.company.getworks.start;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan()
@EnableAutoConfiguration
public class ApplicationMain {

    public static void main(String[] args) {
        SpringApplication.run(ApplicationMain.class, args);
    }

}

和一个带有 @RestContoller 的类,它只包含一个带有 @RequestMapping("/hello") 的方法

我添加了 module-info.java 看起来像

module com.company.getworks.start {
    requires spring.boot;
    requires spring.webmvc;
}

我的build.gradle是

group 'com.company'
version '1.0-SNAPSHOT'

apply plugin: 'java'

sourceCompatibility = 1.9

repositories {
    mavenCentral()
}

dependencies {
    final def springVersion='4.3.10.RELEASE'
    final def springBootVersion='1.5.6.RELEASE'
    testCompile group: 'junit', name: 'junit', version: '4.12'
    compile (group: 'org.springframework', name: 'spring-webmvc', version: springVersion) {
        exclude group: 'commons-logging', module: 'commons-logging'
    }
    compile(group: "org.springframework.boot", name: "spring-boot-starter-web", version: springBootVersion) {
        exclude group: 'commons-logging', module: 'commons-logging'
    }
}

ext.moduleName = 'com.company.getworks.start'

compileJava {
    inputs.property("moduleName", moduleName)
    doFirst {
        options.compilerArgs = [
            '--module-path', classpath.asPath,
            '--add-modules', 'spring.boot',
            '--add-modules', 'spring.webmvc',
    ]
    classpath = files()
    }
}

运行 clean compileJava 以 Build successful 结束,但是当我尝试运行我的 ApplicationMain.java 时,它以 Exception in thread "main"java.lang.IllegalArgumentException: Cannot instantiate interface 结束org.springframework.context.ApplicationContextInitializer : org.springframework.boot.context.ConfigurationWarningsApplicationContextInitializer 引起:java.lang.NoClassDefFoundError: java/sql/SQLException 我做错了什么?

最佳答案

我不得不

1)升级到spring boot 2

2) 将我的依赖项列表更改为

dependencies {
    final def springVersion='4.3.10.RELEASE'
    final def springBootVersion='2.0.0.RELEASE'
    testCompile group: 'junit', name: 'junit', version: '4.12'
    compile (group: 'org.springframework', name: 'spring-webmvc', version: springVersion) {
        exclude group: 'commons-logging', module: 'commons-logging'
    }
    compile(group: "org.springframework.boot", name: "spring-boot-starter-web", version: springBootVersion) {
        exclude group: 'commons-logging', module: 'commons-logging'
        exclude group: 'javax.annotation', module: 'javax.annotation-api'
    }
}

3) 更新module-info.java

module com.company.getworks.start {
    requires spring.boot;
    requires spring.webmvc;
    requires spring.context;
    requires spring.boot.autoconfigure;
    requires spring.web;
    opens com.company.getworks.start;
    exports com.company.getworks.web;
}

com.company.getworks.web 是包含我的@RestController 类的包

4) 正如@miroh 正确提到的那样,将 --add-modules java.sql 添加到我的 java 命令中

关于java - 运行 spring mvc + spring boot gradle java 9 项目,最终出现 java.lang.NoClassDefFoundError : java/sql/SQLException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49087753/

相关文章:

spring - 如何在@Before方法之前执行@Sql

java - Spring 测试配置忽略 Maven 过滤器

Spring 5 在其响应式特性中支持 Java 9 Flow APIs?

ssl - aws elastic beanstalk 上的 Oauth2 服务器

java - 混合 Spring MVC + Spring Data Rest 导致奇怪的 MVC 响应

java - java 桌面中的 session

java - 问 : How to get key value from a HashMap of a ComboBox and save it to the database

java - 如何使用 sapjco3 和 Eclipse 连接到 SAP 系统?

java - 如何向 Feign RequestInterceptor 传递参数?

Java - Zip 输出流动态缓冲区大小