Gradle-线程“主”中的异常java.lang.NoClassDefFoundError:org / slf4j / LoggerFactory

时间:2019-03-08 00:01:48

标签: java eclipse gradle slf4j

成绩文件:

buildscript {
    ext {
        springBootVersion = '1.5.6.RELEASE'
    }
    repositories {
        maven { url 'http://repo1.maven.org/maven2' }
        //maven { url 'https://mvnrepository.com/artifact/org.slf4j/slf4j-api' }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        //compile("org.slfj4:slf4j-api:1.7.25")
        //runtime('org.slfj4:slf4j-simple:1.7.26')

}
}

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.7

repositories {
    maven { url 'http://repo1.maven.org/maven2' }
    //maven { url 'https://mvnrepository.com/artifact/org.slf4j/slf4j-api' }
     }     


dependencies {
    compile('org.springframework.boot:spring-boot-starter-security')
    compile('org.springframework.session:spring-session')
    compile('org.springframework.boot:spring-boot-starter-thymeleaf')
    runtime('org.springframework.boot:spring-boot-devtools')
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    runtime('org.hsqldb:hsqldb')
    runtime('mysql:mysql-connector-java')
    testCompile('org.springframework.boot:spring-boot-starter-test')
    testCompile('org.springframework.security:spring-security-test')
    testCompile('junit:junit:4.12')
    //
//   Dimensions dependencies
//  
    compile group: 'com.serena', name: 'darius', version:'1.0'
    compile group: 'com.serena', name: 'dmfile', version:'1.0'
    compile group: 'com.serena', name: 'dmnet', version:'1.0'
    compile group: 'com.serena', name: 'dmpmcli', version:'1.0'
    compile group: 'com.serena', name: 'dmclient', version:'1.0'

    // log4j
    compile group: 'log4j', name: 'log4j', version: '1.2.17'

// https://mvnrepository.com/artifact/javax.mail/mail
compile group: 'javax.mail', name: 'mail', version: '1.4.1'

// https://mvnrepository.com/artifact/commons-codec/commons-codec
compile group: 'commons-codec', name: 'commons-codec', version: '1.4'

// https://mvnrepository.com/artifact/javax.inject/javax.inject
compile group: 'javax.inject', name: 'javax.inject', version: '1'

//compile group: 'org.slf4j', name: '

}




task setHttpProxyFromEnv {
    def map = ['HTTP_PROXY': 'http', 'HTTPS_PROXY': 'https']
    for (e in System.getenv()) {
        def key = e.key.toUpperCase()
        if (key in map) {
            def base = map[key]
            def url = e.value.toURL()
            println " - systemProp.${base}.proxy=${url.host}:${url.port}"
            System.setProperty("${base}.proxyHost", url.host.toString())
            System.setProperty("${base}.proxyPort", url.port.toString())
        }
    }
}

build.dependsOn setHttpProxyFromEnv

我在eclipse上手动将slj4-api和simple添加到我的类路径中,因此它可以在eclipse上运行,但是当我将其导出到可运行的JAR文件并尝试从命令提示符运行它时,它会不断弹出此错误:

Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory

是应该在命令提示符下运行的命令还是Eclipse中的这个问题

1 个答案:

答案 0 :(得分:1)

我看到您的依赖项中也有log4j。这是否意味着您正在尝试使用slf4j和log4j。

以下是有关如何执行此操作的教程:slf4j-with-log4j

只需将其添加到gradle.build中即可。

  compile group: 'log4j', name: 'log4j', version: '1.2.17'
  compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.26'
  compile group: 'org.slf4j', name: 'slf4j-log4j12', version: '1.7.26'

这是您设置项目目录的方式 Standard Directory Layout

└───maven-project
    ├───pom.xml
    ├───README.txt
    ├───NOTICE.txt
    ├───LICENSE.txt
    └───src
        ├───main
        │   ├───java
        │   └────resources
        │         └───log4j.properties
        └───test
            ├───java
            └───resources

在资源中添加log4j.properties,即所有非Java文件的目录。

log4j.rootCategory=DEBUG, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yy-MM-dd HH:mm:ss:SSS} %5p %t %c{2}:%L - %m%n

但是,该错误可能是由于刷新gradle问题引起的。 放置好依赖项后,尝试同步gradle并清理重建项目。