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

springboot - spring boot项目idea上可以正常运行,打包成jar包后运行出错?

颛孙庆
2024-04-25

一个spring boot的项目,在idea上可以正常运行,打包成jar包后运行出错,报错如下:

2024/04/25-11:33:16 INFO  [main] com.vicy.erp.ErpApplication - Starting ErpApplication v1.0.0-SNAPSHOT on DESKTOP-U2CCDRN with PID 5472 (D:\work\VC2207-medical-erp\src\med-erp\target\vicy-erp-1.0.jar started by Admin in D:\work\VC2207-medical-erp\src\med-erp\target)2024/04/25-11:33:16 DEBUG [main] com.vicy.erp.ErpApplication - Running with Spring Boot v2.0.0.RELEASE, Spring v5.0.4.RELEASE2024/04/25-11:33:16 INFO  [main] com.vicy.erp.ErpApplication - No active profile set, falling back to default profiles: default2024/04/25-11:33:17 ERROR [main] org.springframework.boot.SpringApplication - Application run failedorg.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webConfig.FrontEnd': Unsatisfied dependency expressed through field 'baseDir'; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.io.File'; nested exception is java.lang.IllegalArgumentException: Could not retrieve file for class path resource [erp_web]: class path resource [erp_web] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/D:/work/VC2207-medical-erp/src/med-erp/target/vicy-erp-1.0.jar!/BOOT-INF/classes!/erp_web    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:587)    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:91)    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:373)    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1344)    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582)    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502)    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312)    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310)    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:760)    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:868)    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:752)    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:388)    at org.springframework.boot.SpringApplication.run(SpringApplication.java:327)    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1246)    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1234)    at com.vicy.erp.ErpApplication.main(ErpApplication.java:16)    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)    at java.lang.reflect.Method.invoke(Method.java:498)    at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)    at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)    at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)    at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)Caused by: org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.io.File'; nested exception is java.lang.IllegalArgumentException: Could not retrieve file for class path resource [erp_web]: class path resource [erp_web] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/D:/work/VC2207-medical-erp/src/med-erp/target/vicy-erp-1.0.jar!/BOOT-INF/classes!/erp_web    at org.springframework.beans.TypeConverterSupport.doConvert(TypeConverterSupport.java:80)    at org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java:60)    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1092)    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1065)    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:584)    ... 26 common frames omittedCaused by: java.lang.IllegalArgumentException: Could not retrieve file for class path resource [erp_web]: class path resource [erp_web] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/D:/work/VC2207-medical-erp/src/med-erp/target/vicy-erp-1.0.jar!/BOOT-INF/classes!/erp_web    at org.springframework.beans.propertyeditors.FileEditor.setAsText(FileEditor.java:107)    at org.springframework.beans.TypeConverterDelegate.doConvertTextValue(TypeConverterDelegate.java:466)    at org.springframework.beans.TypeConverterDelegate.doConvertValue(TypeConverterDelegate.java:439)    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:192)    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:117)    at org.springframework.beans.TypeConverterSupport.doConvert(TypeConverterSupport.java:70)    ... 30 common frames omitted

网上的各种方法都试过了,包括加代码、改代码、换一种方式打包,都不行

共有1个答案

羊慈
2024-04-25

报错写了,就是你在读resources文件夹下的文件的时候因为打成jar包的原因读不到了

项目构建成jar的形式之后,resources目录中的文件并不是直接存在系统中,而是嵌套在jar文件中

猜测你用了这样的代码:

xxx = new ClassPathResource(xxx);

假设是这样的:

File file = new ClassPathResource(filePath).getFile();

那你就改成这样:

InputStream fileInputStream = new ClassPathResource(filePath).getInputstream();

再对这个输入流做处理,比如拷贝到另一个地方:

FileUtils.copyToFile(fileInputStream, anotherFile);
 类似资料:
  • 运行正常,打包出错了 。

  • 使用spring Boot开发独立的应用程序,并使用maven构建应用程序。我试图将这个jar文件移动到不同的unix环境中,并尝试部署它。在尝试执行时,我得到了以下错误

  • 本文向大家介绍SpringBoot+Maven 多模块项目的构建、运行、打包实战,包括了SpringBoot+Maven 多模块项目的构建、运行、打包实战的使用技巧和注意事项,需要的朋友参考一下 本篇文章主要介绍了SpringBoot+Maven 多模块项目的构建、运行、打包,分享给大家,具体如下: 项目使用的工具: IntelliJ IDEA JDK 1.8 apache-maven-3.3.9

  • 我有一个基本的SpringBoot应用程序。使用Spring初始值设定项、嵌入式Tomcat、Thymeleaf模板引擎,并将包作为可执行的JAR文件。是一个带有Spring Boot的多模块项目,该项目将有3个模块。这里是父模块pom。xml 这里是模块核心 这里是模块web: 从父根目录,我使用以下方法生成所有模块: 但问题是tdk-web-0.0.2-SNAPSHOT。jar不包含tdk-c

  • 本文向大家介绍maven+springboot打成jar包的方法,包括了maven+springboot打成jar包的方法的使用技巧和注意事项,需要的朋友参考一下 maven的命令: 1.mvn clean package -DskipTests:在项目目录下运行此命令,在target目录下生成jar包或war包。 2.mvn clean:清理项目生产的临时文件,一般是模块下的target目录 3

  • 本文向大家介绍SpringBoot打成war包在tomcat或wildfly下运行的方法,包括了SpringBoot打成war包在tomcat或wildfly下运行的方法的使用技巧和注意事项,需要的朋友参考一下 springboot默认的打包方式为jar包,jar包的有优点是不需要更改的配置,并且内嵌了tomcat,如果打成jar包直接可以丢到docker中运行测试。但是考虑到前端代码只能打成wa