有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

运行SpringMVC+SpringBootGradleJava9项目,并最终使用java。lang.NoClassDefFoundError:java/sql/SQLException

我有一个非常简单的应用程序,可以完美地与Java1.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 com.company.getworks.start {
    requires spring.boot;
    requires spring.webmvc;
}

还有我的身材。格雷德尔是

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以构建成功结束,但当我试图运行我的ApplicationMain时。java它以Exception in thread "main" java.lang.IllegalArgumentException: Cannot instantiate interface org.springframework.context.ApplicationContextInitializer : org.springframework.boot.context.ConfigurationWarningsApplicationContextInitializer Caused by: java.lang.NoClassDefFoundError: java/sql/SQLException结尾 我做错了什么


共 (1) 个答案

  1. # 1 楼答案

    我不得不

    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 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 add modules java。sql到我的java命令