1

I have a file in a location: /resources/static/fcm-admin

It's absolute path: /home/jitu/project-name/src/main/resources/static/fcm-admin

I have tried to access this file in the following ways

val file = ResourceUtils.getFile("classpath:fcm-admin")

It gives me an error java.io.FileNotFoundException: class path resource [fcm-admin] cannot be resolved to an absolute file path because it does not exist

I have tried to access the file in various ways but it is not working. I just want to the access file fcm-admin without giving the full absolute path. Anything will be helpful

EDIT:

So I'm able to access the file on local with the below code -

val file = ResourceUtils.getFile("classpath:static/fcm-admin")

But I'm not able to access it on the production server. And I'm getting below exception

class path resource [static/fcm-admin] cannot be resolved to an absolute file path because it does not reside in the file system: jar:file:/var/app/current/application.jar!/BOOT-INF/classes!/static/apple-app-site-association
Jochen van Wylick
  • 5,303
  • 4
  • 42
  • 64
Jitendra
  • 584
  • 1
  • 10
  • 28
  • Please refer to https://stackoverflow.com/questions/44399422/read-file-from-resources-folder-in-spring-boot/44399541 – dassum May 13 '19 at 13:45

3 Answers3

2

You forgot to add static in the path

val file = ResourceUtils.getFile("classpath:static/fcm-admin")

EDIT because of comment

Load your file from Classpath:

val file = this.javaClass.classLoader.getResource("/static/fcm-admin").file;

When you load a resource using the class loader it will be start in the root of your classpath.

Simon Martinelli
  • 34,053
  • 5
  • 48
  • 82
  • Indeed: `src/main/resources` is the "root" of the classpath, so you need the `static/fcm-admin` as path. – Wim Deblauwe May 13 '19 at 13:48
  • It is working on local but it not working on the server... I'm getting the following error `class path resource [static/fcm-admin] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/var/app/current/application.jar!/BOOT-INF/classes!/static/fcm-admin` – Jitendra May 13 '19 at 14:15
  • Check your jar file on the server. Does the file exist in your jar file? – the hand of NOD May 13 '19 at 14:30
  • @the-hand-of-NOD, I don't have access to that...Is there any other way? – Jitendra May 13 '19 at 14:33
  • @SimonMartinelli, not working... It is generating exception `java.lang.IllegalStateException: this.javaClass.classLoad…urce("/static/fcm-admin") must not be null` – Jitendra May 13 '19 at 14:56
  • Are you sure that the file is in the JAR file? – Simon Martinelli May 13 '19 at 15:07
  • @SimmonMartinelli, I'm not sure, But it is making jar file from the same local code. So it should be there in the jar file. – Jitendra May 13 '19 at 15:34
  • Open the JAR file and have a look if you are not sure – Simon Martinelli May 13 '19 at 16:57
  • In the jar file, it's path is becoming `/BOOT-INF/classes/static/fcm-admin` – Jitendra May 14 '19 at 05:19
  • I cannot help directly with the problem but the 2 links might provide information about how to solve your problem: https://docs.spring.io/spring-boot/docs/current/reference/html/executable-jar.html#executable-jar-jar-file-structure and https://docs.spring.io/spring-boot/docs/current/reference/html/howto-build.html#howto-create-an-additional-executable-jar – the hand of NOD May 14 '19 at 06:25
  • @thehandofNOD, Yeah I understood but the problem is that I'm not able to access the jar file static path..... It is the above comment location. So my question is, how can I access that file in the static folder? – Jitendra May 14 '19 at 08:42
  • 1
    I don't know much about your project setup but you could try: `this.getClass().getResourceAsStream("/static/fcm-admin")` if that does not work you could try to load your resource with the `@Value` spring-boot annotation: `@Value("classpath:/static/fcm-admin")` assuming that fcm-admin is your concrete file you want to load and not just a directory – the hand of NOD May 14 '19 at 09:19
1

Local server can work with ClassPathResource but will fail on production

To solve error on production, change your code to

import org.springframework.core.io.ResourceLoader;
import java.io.InputStream;
import org.springframework.core.io.InputStreamSource;
import org.springframework.core.io.ByteArrayResource;
import org.apache.commons.io.IOUtils;

@Autowired
private ResourceLoader resourceLoader;

InputStream logoFileStrem = resourceLoader.getResource("classpath:static/images/image.png").getInputStream();

InputStreamSource byteArrayResource = new ByteArrayResource(org.apache.commons.io.IOUtils.toByteArray(logoFileStrem));
Chirag Shah
  • 353
  • 1
  • 13
0

I spent so many time, lot of code work on dev but not on prod.

I used SpringBoot with war in production.

To load a file in resources/static the only solution who work in dev and in prod :

val inputStream = Thread.currentThread().contextClassLoader.getResourceAsStream("static/pathToTourFile/nameofFile.extension")
val texte :String =  inputStream.bufferedReader().use(BufferedReader::readText)
Anthone
  • 2,156
  • 21
  • 26