Spring Boot exception: java.lang.NoSuchMethodError

2019-07-11 20:43发布

问题:

I'm able to do a gradle build successfully for a Spring Boot app (spring-boot-gradle-plugin:1.2.6.RELEASE) but I'm getting the exception below when I attempt to run. I believe a similar question has been asked on this but I should be able to run Spring Boot using 1.2.6.RELEASE per the Getting Started guide - http://spring.io/guides/gs/spring-boot/

Any ideas on how to get around this error?

My build.gradle is listed below followed by the exception

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.6.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'

mainClassName = "com.avada.base.Application"

sourceCompatibility = 1.8
targetCompatibility = 1.8

sourceSets {
    main {
        java {
            srcDir 'src'
        }
    }
    test {
        java {
            srcDir 'test'
        }
    }
}

jar {
    baseName = 'IR360'
    version =  '6.0.0'
}

repositories {
    mavenCentral()
}

configurations {
    all*.exclude module : 'spring-boot-starter-logging'
}

dependencies {
    compile ('org.springframework.boot:spring-boot-starter-web')
    compile("org.springframework.boot:spring-boot-starter-actuator")
    testCompile("junit:junit")

    compile fileTree(dir: 'aux-lib', include: '*.jar')
    compile fileTree(dir: 'common/lib', include: '*.jar')
    compile fileTree(dir: 'WebContent/WEB-INF/lib', include: '*.jar')
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.7'
}

 .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.2.6.RELEASE)

Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is java.lang.NoSuchMethodError: org.apache.tomcat.util.scan.StandardJarScanner.setJarScanFilter(Lorg/apache/tomcat/JarScanFilter;)V
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:133)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:474)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:687)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:321)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:967)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:956)
    at com.avada.base.Application.main(Application.java:15)
Caused by: java.lang.NoSuchMethodError: org.apache.tomcat.util.scan.StandardJarScanner.setJarScanFilter(Lorg/apache/tomcat/JarScanFilter;)V
    at org.springframework.boot.context.embedded.tomcat.SkipPatternJarScanner$Tomcat8TldSkipSetter.setSkipPattern(SkipPatternJarScanner.java:106)
    at org.springframework.boot.context.embedded.tomcat.SkipPatternJarScanner.setPatternToTomcat8SkipFilter(SkipPatternJarScanner.java:61)
    at org.springframework.boot.context.embedded.tomcat.SkipPatternJarScanner.<init>(SkipPatternJarScanner.java:56)
    at org.springframework.boot.context.embedded.tomcat.SkipPatternJarScanner.apply(SkipPatternJarScanner.java:87)
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.prepareContext(TomcatEmbeddedServletContainerFactory.java:168)
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.getEmbeddedServletContainer(TomcatEmbeddedServletContainerFactory.java:154)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:157)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:130)
    ... 7 more
:run FAILED

回答1:

You have an old and incompatible version of Tomcat on the classpath. Looking at your build.gradle, it must be coming from one of your fileTree dependencies. You should update their configuration to ensure that the only Tomcat dependencies on the classpath are those that are pulled in by spring-boot-starter-web. If you're not sure where the old Tomcat classes are being loaded from, running the app with -verbose:class will tell you.



回答2:

I may be barking up the wrong tree, however, is there a reason you removed this part of their gradle configuration from the GS guide? (Note the exclude statement):

dependencies {
  // tag::jetty[]
  compile("org.springframework.boot:spring-boot-starter-web") {
    exclude module: "spring-boot-starter-tomcat"
}

From the research I did and from seeing similar Spring errors in the past, if Spring is somehow including the tomcat libs another way via Gradle when you build (Maven scripts don't require this), then the lack of that exclude could be overwriting them, and if the lib which is put in there is not the right version, you could get an error such as this.