gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:48:00 27 4
gpt4 key购买 nike

我有一个非常简单的应用程序,可以完美地与 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com