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

新手上路,请多包涵

一个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.RELEASE
2024/04/25-11:33:16 INFO  [main] com.vicy.erp.ErpApplication - No active profile set, falling back to default profiles: default
2024/04/25-11:33:17 ERROR [main] org.springframework.boot.SpringApplication - Application run failed
org.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 omitted
Caused 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.4k
1 个回答

报错写了,就是你在读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);
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题