3

I am going to read a file available in resources folder in my Springboot application. I have used ResourceLoader to do it. But I get a FileNotFoundException when I try to execute my application.

Error creating bean with name 'demoController': Invocation of init method failed; nested exception is java.io.FileNotFoundException: class path resource [schema.graphql] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/home/dilan/Projects/demo/target/demo.jar!/BOOT-INF/classes!/schema.graphql
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:137) ~[spring-beans-4.3.9.RELEASE.jar!/:4.3.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:409) ~[spring-beans-4.3.9.RELEASE.jar!/:4.3.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1620) ~[spring-beans-4.3.9.RELEASE.jar!/:4.3.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.9.RELEASE.jar!/:4.3.9.RELEASE]

Below is my Code

 @Autowired
private ResourceLoader resourceLoader;

final Resource fileResource = resourceLoader.getResource("classpath:schema.graphql");
File schemaFile = fileResource.getFile();
TypeDefinitionRegistry definitionRegistry = new SchemaParser().parse(schemaFile);
RuntimeWiring wiring = buildRuntimeWiring();

Can anybody please help me to sort this out

enter image description here

Hasitha Jayawardana
  • 2,326
  • 4
  • 18
  • 36
user5489038
  • 31
  • 1
  • 5
  • you're right .. it duplicates another question: https://stackoverflow.com/questions/41754712/spring-boot-reading-text-file-using-resourceloader Please try to even google one time before posting useless questions.. – Yannic Bürgmann Oct 04 '17 at 11:31
  • you cannot get file from a `jar`.. try fetch the file as stream.. – Jaiwo99 Oct 04 '17 at 11:36

3 Answers3

4

You could use ClassPathResource

Smth like this :

InputStream inputStream = new ClassPathResource("schema.graphql").getInputStream();

Or

File file = new ClassPathResource("schema.graphql").getFile();
Aliaksei Stadnik
  • 1,692
  • 3
  • 15
  • 32
0

try to use the code below to access files in ressources folder with spring boot

 File file = new ClassPathResource("schema.graphql").getFile();
0

Adding the answer from here, Accessing the ClassPathResource and copying the content to a String

try {
   ClassPathResource classPathResource = new ClassPathResource("schema.graphql");
   byte[] data = FileCopyUtils.copyToByteArray(classPathResource.getInputStream());
   String content = new String(data, StandardCharsets.UTF_8);
} catch (Exception ex) {
  ex.printStackTrace();
}
Hasitha Jayawardana
  • 2,326
  • 4
  • 18
  • 36