简体   繁体   中英

Gradle - Exception in thread “main” java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory

Gradle file:

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

I added the slj4-api and simple into my classpath, manually on eclipse, so it works on eclipse, however when I export it into a runnable JAR File and try to run it from the command prompt it keeps popping up this error:

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

Is there a commands that supposed to be ran on the command prompt or is this issue within eclipse

I see that you also have log4j in your dependency. Does that mean you are trying to use slf4j and log4j.

Here is a tutorial on how to do that: slf4j-with-log4j

Simply you need to add this in your 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'

this is how you set up your project directory Standard Directory Layout

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

add log4j.properties in your resources, thats the directory for all your non java files.

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

The error however can be due a refresh gradle issue. after you have your dependencies in place, Try to sync gradle and clean rebuild your project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM