spring-boot 战争在启动时失败

Posted

技术标签:

【中文标题】spring-boot 战争在启动时失败【英文标题】:spring-boot war fails on startup 【发布时间】:2015-05-09 13:29:45 【问题描述】:

我正在尝试使用带有 gretty 的 spring-boot 创建 Web 服务。在gradle中调用appStart时效果很好,但是调用appStartWar时会报错。

这是错误:

Exception in thread "main" java.lang.ClassNotFoundException: com.example.server.Application
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:270)
    at java_lang_Class$forName.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:124)
    at org.akhikhl.gretty.SpringBootServerManager.startServer(SpringBootServerManager.groovy:46)
    at org.akhikhl.gretty.ServerManager$startServer$0.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
    at org.akhikhl.gretty.Runner.run(Runner.groovy:70)
    at org.akhikhl.gretty.Runner.this$2$run(Runner.groovy)
    at org.akhikhl.gretty.Runner$this$2$run.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:112)
    at org.akhikhl.gretty.Runner.main(Runner.groovy:48)
Exception in thread "Thread-18" org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.7.0_51\bin\java.exe'' finished with non-zero exit value 1
    at org.gradle.process.internal.DefaultExecHandle$ExecResultImpl.assertNormalExitValue(DefaultExecHandle.java:365)
    at org.gradle.process.internal.DefaultJavaExecAction.execute(DefaultJavaExecAction.java:31)
    at org.gradle.api.internal.file.DefaultFileOperations.javaexec(DefaultFileOperations.java:159)
    at org.gradle.api.internal.project.AbstractProject.javaexec(AbstractProject.java:793)
    at org.gradle.api.Project$javaexec$8.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
    at org.akhikhl.gretty.DefaultLauncher.javaExec(DefaultLauncher.groovy:48)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
    at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:324)
    at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:368)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1016)
    at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:66)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:49)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:133)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:141)
    at org.akhikhl.gretty.LauncherBase$_launchThread_closure6.doCall(LauncherBase.groovy:216)
    at org.akhikhl.gretty.LauncherBase$_launchThread_closure6.doCall(LauncherBase.groovy)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
    at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:324)
    at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:278)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1016)
    at groovy.lang.Closure.call(Closure.java:423)
    at groovy.lang.Closure.call(Closure.java:417)
    at groovy.lang.Closure.run(Closure.java:504)
    at java.lang.Thread.run(Thread.java:744)

好像在war文件中找不到主类,但是com.example.server.Application确实存在于war中,路径为WEB-INF/classes/com/example/server

build.gradle

apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'spring-boot'
apply plugin: 'org.akhikhl.gretty'

buildscript 
    repositories 
        mavenCentral()
        jcenter()
    
    dependencies 
        classpath 'org.akhikhl.gretty:gretty:1.1.8'
        classpath 'org.springframework.boot:spring-boot-gradle-plugin:1.2.2.RELEASE'
    


dependencies

    compile 'org.springframework.boot:spring-boot-starter-web:1.2.2.RELEASE'

    providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
    providedCompile 'javax.servlet:javax.servlet-api:3.1.0'



gretty 
    springBoot = true
    springBootVersion = '1.2.2.RELEASE'
    httpsEnabled = true

Application.java

@RestController
@SpringBootApplication
class Application extends SpringBootServletInitializer 

    public static void main( String[] args ) 
        SpringApplication.run( Application.class, args );
    

    @Override
    protected SpringApplicationBuilder configure( SpringApplicationBuilder builder ) 
        return builder.sources( Application.class );
    

    @RequestMapping( value = "test", method = RequestMethod.GET )
    public String test() 
        return "success";
    



任何帮助表示赞赏:)


这是我在 Gitlab 中的项目link


我尝试直接将war部署到tomcat-7,但也失败了。所以我认为这不是一个严重的问题。

日志

javax.servlet.ServletException: Failed to instantiate WebApplicationInitializer class
    at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:160)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5513)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:649)
    at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:1081)
    at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1877)
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IllegalAccessException: Class org.springframework.web.SpringServletContainerInitializer can not access a member of class com.example.server.Application with modifiers ""
    at sun.reflect.Reflection.ensureMemberAccess(Unknown Source)
    at java.lang.Class.newInstance(Unknown Source)
    at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:157)
    ... 12 more

【问题讨论】:

无法重现,能否提供一个最小的工作示例? @Opal 谢谢你的帮助!我在上面添加了我的示例项目链接。 转载了,可惜不知道解决办法。 【参考方案1】:

你的

@RestController
@SpringBootApplication
class Application extends SpringBootServletInitializer

类需要公开

@RestController
@SpringBootApplication
public class Application extends SpringBootServletInitializer

在其当前形式中,它是包保护的,不能通过反射实例化。 (默认)

【讨论】:

谢谢,它现在适用于 tomcat。但 gretty appStartWar 仍然无法正常工作。 如果您仍然遇到相同的错误(由于您的反射问题),那么您需要进行大量清理。我不知道 gretty,但尽你所能:清理/重建/重新部署。如果您所做的代码更改未正确部署,并且仍在使用旧实现,那么您的问题仍然存在的唯一合乎逻辑的解释。

以上是关于spring-boot 战争在启动时失败的主要内容,如果未能解决你的问题,请参考以下文章

spring-boot:如果密码失败,则启动没有数据库的应用程序

Spring-boot 启动报错 调试小技巧

无法在一台特定服务器上部署战争

谈谈spring-boot不同包结构下,同样的类名冲突导致服务启动失败解决方案

在 Glassfish 中部署战争文件失败

spring-boot结合maven配置不同环境的profile