当前位置: 首页 > 知识库问答 >
问题:

用Gradle和Spring-boot构建可执行jar文件时遇到的问题

封昊天
2023-03-14

我在执行构建的jar文件时遇到了麻烦:

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

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

我在这里遵循了以下说明:http://docs.spring.io/spring-boot/docs/current/reference/html/build-tool-plugins-gradle-plugin.html

我的建筑。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')
}

谢谢你。

共有1个答案

詹高畅
2023-03-14

如果不缺少一些元素,回答起来可能有点困难;如果你有一些花哨的东西,请突出显示或更好地分享你的应用程序,但基本上我能够使它与以下工作:

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版本的文档是http://docs.spring.io/spring-boot/docs/1.2.6.release/reference/html/build-tool-plugins-gradle-plugin.html#build-tool-plugins-gradle-dependencies-now-versions,它们确实提到了添加依赖项。

脚本上的一个说明,难道selenium不只是测试所必需的吗?您可以将编译文件..selenium更改为testcompile文件..selenium(甚至testruntime)您的jar文件将为此感谢您。

为了便于了解,我为这个测试构建了一个非常基本的Spring-Boot应用程序(基于Spring Boot In Action中的示例)

package com.readinglist;

import org.apache.log4j.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.openqa.selenium.server.SeleniumServer;

@SpringBootApplication
public class ReadingListApplication {

    private static Logger log = Logger.getLogger(ReadingListApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(ReadingListApplication.class, args);
        log.info("SeleniumServer ClassName is " + SeleniumServer.class.getName());
    }
}

重要的是确保您有一个有效的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

如果有错误,请确保在做了一些更改后运行gradle clean

 类似资料:
  • 我试图在Spring Boot+Gradle项目中构建一个可执行的jar,但目前没有任何工作。下面是最简单的结构。可能,Gradle配置中缺少一些东西。 格雷德尔: 主配置文件: 当我运行java-jar1.jar这样的jar文件时,出现了以下异常: 可能出了什么问题?

  • 我已经找了两天来解决这个问题,但我没有找到解决办法。首先我想构建fat-jar,然后我放弃了它看起来更复杂,所以我开始构建普通的可执行Jar。我正在用Gradle插件在NetBeans 8.2中构建一些程序(http://plugins.NetBeans.org/plugin/44510/gradle-support) 我的项目结构是这样的: 项目结构 我是一个比较新的年级,我使用它不到一个月。我

  • 我想建一个图书馆。我的build.Gradle文件中有Spring Boot Gradle插件: 我的库没有主类。为防止“主类未配置...”错误,我将其添加到Build.Gradle: 在org.gradle.api.project类型的根项目“MySuperLibrary”上找不到参数[build_3886uiniuyuorta9lpa9n4f5c$_run_closure3@1975ec48]

  • 我有一个用Spring Boot的Gradle插件构建的可执行JAR。它在生成文件中具有以下依赖项: 建筑格拉德尔 问题是,在我运行之后,创建的工件不包含jar,只包含其他的tivemq jars: 因此,当我尝试运行jar时,我遇到了一个异常,即无法找到此依赖项中的类: 在构建过程中,依赖被下载,它可以在我的gradle缓存中找到,似乎出于某种原因,Spring Boot的Gradle插件在引导

  • 我目前正在阅读Spring Boot的教程页(https://Spring.io/guides/gs/uploading-files/#initial),以便检查如何上传映像文件,但我不太理解“运行应用程序”一章的“构建可执行JAR”部分。 我通过教程页面上的链接下载了该项目,并复制和粘贴了所有文件,我在命令提示符上编写了,就像在构建页面中编写的可执行JAR部分一样,但我无法运行该程序。 为了更具

  • 我很难让JavaFX应用程序在IDE之外运行。我正在使用IntelliJ IDEA 2019.3,并使用OpenJDK-12进行编译。JavaFX sdk版本为11.0.2,我使用外部库JFoenix-9.0.8和JSerialComm-2.6.0。操作系统是Windows10,安装了JDK14,我可以运行使用命令行以相同方式构建的非JavaFX应用程序。当我尝试从命令行运行mu JavaFX.j