Java 使用Gradle和Spring引导构建可执行jar文件时出现问题

Java 使用Gradle和Spring引导构建可执行jar文件时出现问题,java,gradle,dependencies,spring-boot,Java,Gradle,Dependencies,Spring Boot,我在执行构建的jar文件时遇到问题: 运行以下命令java-jar/build/libs/***.jar会给我带来一系列NoClassDefFoundError错误 但是当我运行gradle bootrun时,它运行得很好 我按照这里的指示: 我的build.gradle如下所示: apply plugin: 'spring-boot' sourceCompatibility = 1.5 version = '1.0' buildscript { repositories {

我在执行构建的jar文件时遇到问题:

运行以下命令
java-jar/build/libs/***.jar
会给我带来一系列
NoClassDefFoundError
错误

但是当我运行gradle bootrun时,它运行得很好

我按照这里的指示:

我的build.gradle如下所示:

apply plugin: 'spring-boot'

sourceCompatibility = 1.5
version = '1.0'

buildscript {
    repositories {
        jcenter()
    }

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

springBoot {
    mainClass = "org.gradle.App"
}

dependencies {
    compile files('lib/selenium-server-standalone-2.47.1.jar')
}

谢谢。

如果没有遗漏一些元素,回答可能有点困难;如果您有一些新奇的东西,请突出显示或更好地共享您的应用程序,但基本上我能够通过以下方式使其工作:

build.gradle文件与您的非常相似

apply plugin: 'spring-boot'

sourceCompatibility = 1.5
version = '1.0'

buildscript {
    repositories {
        jcenter()
    }

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

springBoot {
    mainClass = "com.readinglist.ReadingListApplication"
}

repositories {
    mavenCentral()
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    compile files('lib/selenium-server-standalone-2.47.1.jar')
    testCompile('org.springframework.boot:spring-boot-starter-test') 
}
我不知道它们是否有很大的不同,但是您的1.2.6版本的文档是,他们提到添加依赖项

脚本上的一个注释,selenium不仅仅是测试所必需的吗?您可以将
compile files..selenium
更改为
testCompile files..selenium
(甚至
testRuntime
),您的jar文件将为此感谢您

我为这个测试构建了一个非常基本的SpringBoot应用程序(基于SpringBoot中的示例)

重要的一点是确保您有一个有效的
main
方法

作为参考,我的gradle执行是

fhenri@machine:~/project/examples/spring-boot/inactionbook/readinglist$ gradle build
:compileJava
warning: [options] bootstrap class path not set in conjunction with -source 1.5
warning: [options] source value 1.5 is obsolete and will be removed in a future release
warning: [options] target value 1.5 is obsolete and will be removed in a future release
warning: [options] To suppress warnings about obsolete options, use -Xlint:-options.
4 warnings
:processResources
:classes
:jar
:findMainClass
:startScripts
:distTar
:distZip
:bootRepackage
:assemble
:compileTestJava
warning: [options] bootstrap class path not set in conjunction with -source 1.5
warning: [options] source value 1.5 is obsolete and will be removed in a future release
warning: [options] target value 1.5 is obsolete and will be removed in a future release
warning: [options] To suppress warnings about obsolete options, use -Xlint:-options.
4 warnings
:processTestResources UP-TO-DATE
:testClasses
:test
:check
:build

BUILD SUCCESSFUL

Total time: 5.411 secs
最后运行你的命令

fhenri@machine:~/project/examples/spring-boot/inactionbook/readinglist$ java -jar build/libs/***.jar

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

2015-10-13 19:18:01.625  INFO 94280 --- [           main] com.readinglist.ReadingListApplication   : Starting ReadingListApplication on macbook-pro-de-frederic.home with PID 94280 (/Users/fhenri/project/examples/spring-boot/inactionbook/readinglist/build/libs/readinglist-1.0.jar started by fhenri in /Users/fhenri/project/examples/spring-boot/inactionbook/readinglist)
2015-10-13 19:18:01.679  INFO 94280 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@71c79795: startup date [Tue Oct 13 19:18:01 CEST 2015]; root of context hierarchy
2015-10-13 19:18:02.377  INFO 94280 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'beanNameViewResolver': replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]]
2015-10-13 19:18:03.141  INFO 94280 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2015-10-13 19:18:03.489  INFO 94280 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2015-10-13 19:18:03.491  INFO 94280 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.0.26
2015-10-13 19:18:04.727  INFO 94280 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2015-10-13 19:18:04.727  INFO 94280 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 3053 ms
2015-10-13 19:18:05.379  INFO 94280 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'dispatcherServlet' to [/]
2015-10-13 19:18:05.385  INFO 94280 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'characterEncodingFilter' to: [/*]
2015-10-13 19:18:05.386  INFO 94280 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2015-10-13 19:18:05.692  INFO 94280 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@71c79795: startup date [Tue Oct 13 19:18:01 CEST 2015]; root of context hierarchy
2015-10-13 19:18:05.757  INFO 94280 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2015-10-13 19:18:05.758  INFO 94280 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest)
2015-10-13 19:18:05.786  INFO 94280 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2015-10-13 19:18:05.787  INFO 94280 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2015-10-13 19:18:05.824  INFO 94280 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2015-10-13 19:18:05.940  INFO 94280 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2015-10-13 19:18:06.047  INFO 94280 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2015-10-13 19:18:06.050  INFO 94280 --- [           main] com.readinglist.ReadingListApplication   : Started ReadingListApplication in 4.751 seconds (JVM running for 5.476)
2015-10-13 19:18:06.052  INFO 94280 --- [           main] com.readinglist.ReadingListApplication   : SeleniumServer ClassName is org.openqa.selenium.server.SeleniumServer
fhenri@machine:~/project/examples/spring boot/inactionbook/readinglist$java-jarbuild/libs/***.jar
.   ____          _            __ _ _
/\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/  ___)| |_)| | | | | || (_| |  ) ) ) )
'  |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
::弹簧靴::(v1.2.6.版本)
2015-10-13 19:18:01.625信息94280---[main]com.readinglist.readinglist应用程序:开始在macbook-pro-de-frederic.home上使用PID 94280读取列表应用程序(/Users/fhenri/project/examples/spring boot/inactionbook/readinglist/build/libs/readinglist-1.0.jar由fhenri在/Users/fhenri/project/examples/spring boot/inactionbook/readinglist中启动)
2015-10-13 19:18:01.679信息94280---[main]国家配置嵌入式Web应用程序上下文:刷新org.springframework.boot.context.embedded。AnnotationConfigEmbeddedWebApplicationContext@71c79795:启动日期[Tue Oct 13 19:18:01 CEST 2015];上下文层次结构的根
2015-10-13 19:18:02.377信息94280---[main]o.s.b.f.s.DefaultListableBeanFactory:重写bean“beanNameViewResolver”的bean定义:替换[Root bean:class[null];scope=;abstract=false;lazyInit=false;autowireMode=3;dependencyCheck=0;autowireCandidate=true;primary=false;factoryBeanName=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhiteLabeleErrorViewConfiguration;factoryMethodName=BeanNameResolver;initMethodName=null;destroyMethodName=(推断);在类路径资源[org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class]]中用[Root bean:class[null]定义;scope=;abstract=false;lazyInit=false;autowireMode=3;dependencyCheck=0;autowireCandidate=true;primary=false;factoryBeanName=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfiguration适配器;factoryMethodName=beanNameViewResolver;initMethodName=null;destroyMethodName=(推断);在类路径资源[org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]中定义
2015-10-13 19:18:03.141信息94280---[main]s.b.c.e.t.TomcatEmbeddedServletContainer:Tomcat用端口初始化:8080(http)
2015-10-13 19:18:03.489信息94280---[main]o.apache.catalina.core.StandardService:启动服务Tomcat
2015-10-13 19:18:03.491信息94280---[main]org.apache.catalina.core.StandardEngine:启动Servlet引擎:ApacheTomcat/8.0.26
2015-10-13 19:18:04.727信息94280---[ost-startStop-1]o.a.c.c.c.[Tomcat].[localhost].[/]:初始化Spring嵌入式WebApplicationContext
2015-10-13 19:18:04.727信息94280---[ost-startStop-1]o.s.web.context.ContextLoader:根WebApplicationContext:初始化在3053毫秒内完成
2015-10-13 19:18:05.379信息94280---[ost-startStop-1]o.s.b.c.e.ServletRegistrationBean:将servlet:“dispatcherServlet”映射到[/]
2015-10-13 19:18:05.385信息94280---[ost-startStop-1]o.s.b.c.embedded.FilterRegistrationBean:将筛选器:“characterEncodingFilter”映射到:[/*]
2015-10-13 19:18:05.386信息94280---[ost-startStop-1]o.s.b.c.embedded.FilterRegistrationBean:将筛选器:“hiddenHttpMethodFilter”映射到:[/*]
2015-10-13 19:18:05.692信息94280---[main]s.w.s.m.a.RequestMappingHandlerAdapter:正在寻找@ControllerAdvice:org.springframework.boot.context.embedded。AnnotationConfigEmbeddedWebApplicationContext@71c79795:启动日期[Tue Oct 13 19:18:01 CEST 2015];上下文层次结构的根
2015-10-13 19:18:05.757 INFO 94280-[main]s.w.s.m.a.RequestMappingHandlerMapping:将“{[/error]}”映射到公共组织springframework.http.ResponseEntity org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2015-10-13 19:18:05.758 INFO 94280-[main]s.w.s.m.m.a.RequestMappingHandlerMapping:将“{[/error],products=[text/html]}”映射到public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest)
2015-10-13 19:18:05.786 INFO 94280---[main]o.s.w.s.handler.simplerlhandler映射:将URL路径[/webjars/**]映射到[class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]类型的处理程序上
2015-10-13 19:18:05.787信息94280---[main]o.s.w.s.handler.simplerlhandler映射:映射URL
fhenri@machine:~/project/examples/spring-boot/inactionbook/readinglist$ java -jar build/libs/***.jar

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

2015-10-13 19:18:01.625  INFO 94280 --- [           main] com.readinglist.ReadingListApplication   : Starting ReadingListApplication on macbook-pro-de-frederic.home with PID 94280 (/Users/fhenri/project/examples/spring-boot/inactionbook/readinglist/build/libs/readinglist-1.0.jar started by fhenri in /Users/fhenri/project/examples/spring-boot/inactionbook/readinglist)
2015-10-13 19:18:01.679  INFO 94280 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@71c79795: startup date [Tue Oct 13 19:18:01 CEST 2015]; root of context hierarchy
2015-10-13 19:18:02.377  INFO 94280 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'beanNameViewResolver': replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]]
2015-10-13 19:18:03.141  INFO 94280 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2015-10-13 19:18:03.489  INFO 94280 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2015-10-13 19:18:03.491  INFO 94280 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.0.26
2015-10-13 19:18:04.727  INFO 94280 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2015-10-13 19:18:04.727  INFO 94280 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 3053 ms
2015-10-13 19:18:05.379  INFO 94280 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'dispatcherServlet' to [/]
2015-10-13 19:18:05.385  INFO 94280 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'characterEncodingFilter' to: [/*]
2015-10-13 19:18:05.386  INFO 94280 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2015-10-13 19:18:05.692  INFO 94280 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@71c79795: startup date [Tue Oct 13 19:18:01 CEST 2015]; root of context hierarchy
2015-10-13 19:18:05.757  INFO 94280 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2015-10-13 19:18:05.758  INFO 94280 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest)
2015-10-13 19:18:05.786  INFO 94280 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2015-10-13 19:18:05.787  INFO 94280 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2015-10-13 19:18:05.824  INFO 94280 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2015-10-13 19:18:05.940  INFO 94280 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2015-10-13 19:18:06.047  INFO 94280 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2015-10-13 19:18:06.050  INFO 94280 --- [           main] com.readinglist.ReadingListApplication   : Started ReadingListApplication in 4.751 seconds (JVM running for 5.476)
2015-10-13 19:18:06.052  INFO 94280 --- [           main] com.readinglist.ReadingListApplication   : SeleniumServer ClassName is org.openqa.selenium.server.SeleniumServer