UnsupportedClassVersionError:javax / annotation / ManagedBean:不支持的major.minor版本51.0

时间:2018-04-22 15:26:03

标签: gradle

我在STS上开发一个spring boot 1.4.2应用程序,部署在weblogic 10(j2ee 5,j-se 6)上,当试图以Spring启动应用程序运行应用程序时出现此错误:

java.lang.UnsupportedClassVersionError: javax/annotation/ManagedBean : Unsupported major.minor version 51.0
    at java.lang.ClassLoader.defineClass1(Native Method) ~[na:1.6.0_45]
    at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631) ~[na:1.6.0_45]
    at java.lang.ClassLoader.defineClass(ClassLoader.java:615) ~[na:1.6.0_45]
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141) ~[na:1.6.0_45]
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:283) ~[na:1.6.0_45]
    at java.net.URLClassLoader.access$000(URLClassLoader.java:58) ~[na:1.6.0_45]
    at java.net.URLClassLoader$1.run(URLClassLoader.java:197) ~[na:1.6.0_45]
    at java.security.AccessController.doPrivileged(Native Method) ~[na:1.6.0_45]
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190) ~[na:1.6.0_45]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306) ~[na:1.6.0_45]
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) ~[na:1.6.0_45]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247) ~[na:1.6.0_45]
    at org.springframework.util.ClassUtils.forName(ClassUtils.java:250) ~[spring-core-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.registerDefaultFilters(ClassPathScanningCandidateComponentProvider.java:243) ~[spring-context-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.<init>(ClassPathScanningCandidateComponentProvider.java:116) ~[spring-context-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.<init>(ClassPathBeanDefinitionScanner.java:136) ~[spring-context-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.<init>(ClassPathBeanDefinitionScanner.java:111) ~[spring-context-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.<init>(ClassPathBeanDefinitionScanner.java:83) ~[spring-context-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext.<init>(AnnotationConfigEmbeddedWebApplicationContext.java:67) ~[spring-boot-1.4.2.RELEASE.jar:1.4.2.RELEASE]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.6.0_45]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) ~[na:1.6.0_45]
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) ~[na:1.6.0_45]
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513) ~[na:1.6.0_45]
    at java.lang.Class.newInstance0(Class.java:357) ~[na:1.6.0_45]
    at java.lang.Class.newInstance(Class.java:310) ~[na:1.6.0_45]
    at org.springframework.beans.BeanUtils.instantiate(BeanUtils.java:77) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.boot.SpringApplication.createApplicationContext(SpringApplication.java:597) [spring-boot-1.4.2.RELEASE.jar:1.4.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:311) [spring-boot-1.4.2.RELEASE.jar:1.4.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1186) [spring-boot-1.4.2.RELEASE.jar:1.4.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1175) [spring-boot-1.4.2.RELEASE.jar:1.4.2.RELEASE]
    at com.ejada.staripo.OnlineChannelBackendApplication.main(OnlineChannelBackendApplication.java:22) [main/:na]

我知道错误的原因(编译时的旧版本,运行时的新版本)...我无法弄清楚导致错误的原因...我应该用java 6编译我的应用程序。 ..但是错误说是使用java 7编译的东西

gradle guild文件:

buildscript {
    ext {
        springBootVersion = '1.4.2.RELEASE'
    }
    repositories {
        mavenCentral()
        mavenLocal()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse-wtp'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'war'

group = 'com.mypack'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.6 //java6

repositories {
    mavenCentral()
    mavenLocal()
}

configurations {
    providedRuntime
    //jaxb
}

// tag::wsdl[]
//  task genJaxb {
//      ext.sourcesDir = "${projectDir}/src/main/java"
//      ext.classesDir = "${buildDir}/classes/jaxb"
//      ext.schema1 = "src/main/resources/com/ejada/staripo/wsdl/ELMCitizenInfoInq.wsdl"
//      ext.src1 = "${projectDir}/src/main/java/com/ejada/staripo/backend/elm/citizeninfo"
//      ext.schema2 = "src/main/resources/com/ejada/staripo/wsdl/ELMMuqeemInfoInq.wsdl"
//      ext.src2 = "${projectDir}/src/main/java/com/ejada/staripo/backend/elm/muqeeminfo"
//
//      outputs.dir classesDir
//
//      doLast() {
//          project.ant {
//              taskdef name: "xjc", classname: "com.sun.tools.xjc.XJCTask",
//                      classpath: configurations.jaxb.asPath
//              mkdir(dir: sourcesDir)
//              mkdir(dir: classesDir)
//
//              xjc(destdir: sourcesDir, schema: schema1,
//                      package: "com.ejada.staripo.backend.elm.citizeninfo") {
//                  arg(value: "-wsdl")
//                  produces(dir: sourcesDir, includes: "**/*.java")
//              }
//
//              xjc(destdir: sourcesDir, schema: schema2,
//                      package: "com.ejada.staripo.backend.elm.muqeeminfo") {
//                  arg(value: "-wsdl")
//                  produces(dir: sourcesDir, includes: "**/*.java")
//              }
//
//
//              javac(destdir: classesDir, source: 1.8, target: 1.8, debug: true,
//                      debugLevel: "lines,vars,source",
//                      classpath: configurations.jaxb.asPath) {
//                  src(path: src1)
//                  include(name: "**/*.java")
//                  include(name: "*.java")
//              }
//
//              copy(todir: classesDir) {
//                  fileset(dir: src1, erroronmissingdir: false) {
//                      exclude(name: "**/*.java")
//                  }
//              }
//              
//                          javac(destdir: classesDir, source: 1.8, target: 1.8, debug: true,
//                      debugLevel: "lines,vars,source",
//                      classpath: configurations.jaxb.asPath) {
//                  src(path: src2)
//                  include(name: "**/*.java")
//                  include(name: "*.java")
//              }
//
//              copy(todir: classesDir) {
//                  fileset(dir: src2, erroronmissingdir: false) {
//                      exclude(name: "**/*.java")
//                  }
//              }
//          }
//      }
//  }
// end::wsdl[]

dependencies {
    compile('org.springframework.boot:spring-boot-legacy:1.1.0.RELEASE')
    compile('org.springframework.boot:spring-boot-starter-web:1.4.2.RELEASE')
    compile('com.fasterxml.jackson.core:jackson-databind:2.6.7') //java6
    compile('org.springframework.ws:spring-ws-core')
    compile('org.mybatis.spring.boot:mybatis-spring-boot-starter:1.2.0')
    compile('org.projectlombok:lombok:1.16.10')
    compile('com.oracle:ojdbc6:11.1.0.7.0') //java6
    //compileOnly('org.springframework.boot:spring-boot-devtools')
    compileOnly('javax.servlet:servlet-api:2.5')
    //compile(files(genJaxb.classesDir).builtBy(genJaxb))

    //jaxb "com.sun.xml.bind:jaxb-xjc:2.1.7"
    providedRuntime('org.springframework.boot:spring-boot-starter-tomcat') // commented for java6
    providedRuntime('org.apache.tomcat:tomcat-juli:7.0.59') // java6
    //compileOnly group:'org.apache.tomcat', name:'tomcat-juli', version:'7.0.59' // java6
    testCompile('org.springframework.boot:spring-boot-starter-test')
    //testCompile('org.springframework.security:spring-security-test')
}

    /*compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile('org.springframework.boot:spring-boot-starter-data-rest')
    compile('org.springframework.boot:spring-boot-starter-security')
    compile('org.springframework.session:spring-session-core')
    */

//bootJar { //commented 4 java6
  //  baseName = 'gs-consuming-web-service'
    //version =  '0.1.0'

  //  from genJaxb.classesDir
//}


//task afterEclipseImport {
  //  dependsOn genJaxb
//}

这里是生成的战争中的lib文件夹:

enter image description here

3 个答案:

答案 0 :(得分:0)

首先,sourceCompatibility仅指定代码的源兼容性。要生成所需版本的.class个文件,您应该使用targetCompatibility。请参阅this answer

其次,你的错误原因是:

  

java.lang.UnsupportedClassVersionError:javax / annotation / ManagedBean:不支持的major.minor版本51.0

不是:

  

编译时的旧版本,运行时的新版本

但是:编译时的新版本,旧版本的jvm

第三,javax.annotation.ManagedBean不是从您的源代码编译的,而是 Javax Annotation API 的一部分,您应该选择兼容java-6的jar。

答案 1 :(得分:0)

我所做的是我制作了一场战争并将其部署到外部的tonmcat 7而不是嵌入式的......由于某种原因,我不知道的错误更清楚......它直接指向了导致错误的包,它是在spring-ws-core,因此我需要使用版本2.2.4.RELEASE

答案 2 :(得分:0)

您需要在Java 6的构建脚本中将tomcat版本设置为7.您可以在Spring Boot文档中找到一些其他配置。 https://docs.spring.io/spring-boot/docs/1.4.4.RELEASE/reference/htmlsingle/#howto-use-java-6

例如:Gradle脚本

ext['tomcat.version'] = '7.0.59'

我希望这有帮助。