Reading files from a SpringBoot jar is something a little complex.
I had tried to read a file from springboot resources folder using java.io.File
. This is the code:
1 2 3 4 5 6 |
public static void readFromResources() { String path = "config.xml"; String fullPath = IoKit.class.getClassLoader().getResource(path).getFile(); File file = new File(fullPath); System.out.println(file.exists()); } |
And I got the FileNotFoundException
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
java.io.FileNotFoundException: class path resource [config.xml] cannot be resolved to absolute file path because it does not reside in the file system: file:/data/www/htdocs/my-app/lib/my-app.jar!/BOOT-INF/class es!/config.xml at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:217) at org.springframework.core.io.AbstractFileResolvingResource.getFile(AbstractFileResolvingResource.java:154) at org.chobit.IoKit.readResources(IoKit.java:46) at org.chobit.IoKitTest.readResources(IoKitTest.java:67) 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.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:893) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:798) at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) |
Since there is no path like file:/data/www/htdocs/my-app/lib/my-app.jar!/BOOT-INF/class
existed in my computer, I got the exception. Continue reading