14

My Code:

XWPFDocument doc = new XWPFDocument(OPCPackage.open(ResourceUtils.getFile("classpath:assets/OPTIONS_" + jubilar1.getJubiLanguage().toUpperCase() + ".docx")));

I have already tried instead of .getFile(), extractJarFileFromURL or resource.getInputStream() but all this does not work. When I package my project and run it as a jar file and it tries to open the following file it always returns the following message.

Error:

java.io.FileNotFoundException: class path resource [assets/OPTIONS_DE. docx] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/home/tkf6y/IdeaProjects/hrapps/backend/target/backend-3.0.0.jar!/BOOT-INF/classes!/assets/OPTIONS_EN.docx

Jens
  • 67,715
  • 15
  • 98
  • 113
joni
  • 231
  • 1
  • 3
  • 7
  • 8
    The error isn't wrong. It isn't a `java.util.File` as it isn't a physical file on the file system it is part of a jar. You can use the `InputStream` (and should) to read it regardless of the location. There is an `OPCPackage.open` method which takes an `Inputstream`. – M. Deinum Jan 05 '22 at 13:28
  • 1
    No, that was not the problem. However, I have solved it now. I changed my code line to the following: `code`XWPFDocument doc = new XWPFDocument(OPCPackage.open(getClass().getResourceAsStream("/assets/OPTIONS_" + jubilar1.getJubiLanguage().toUpperCase() + ".docx"))); `code` I removed ResourceUtils.getFile() and used instead getClass().getResourceAsStream(). Then you can also do it without classpath:. – joni Jan 06 '22 at 07:18
  • 3
    So yes it was the problem, as you are now using an `InputStream` as I suggested. The problem was (and always has been) the `getFile` stuff. What I suggest to do is don't use what you have now but rather do a `new ClassPathResource(your location).getInputStream())` instead, it is easier, or even use a `ResourceLoader` (a Spring interface you can inject) and then use the path you had an again use `getInputStream()`. – M. Deinum Jan 06 '22 at 07:26

2 Answers2

9

So yes it was the problem, as you are now using an InputStream as I suggested. The problem was (and always has been) the getFile stuff. What I suggest to do is don't use what you have now but rather do a new ClassPathResource(your location).getInputStream()) instead, it is easier, or even use a ResourceLoader (a Spring interface you can inject) and then use the path you had an again use getInputStream(). –

joni
  • 231
  • 1
  • 3
  • 7
  • Hi @joni do you know how to create a file while my spring application is in the docker container? If you can answer my question, thanks!! – Sonn Aug 28 '22 at 07:53
0

This works for me.

        String strJson = null;
        ClassPathResource classPathResource = new ClassPathResource("json/data.json");
        try {
            byte[] binaryData = FileCopyUtils.copyToByteArray(classPathResource.getInputStream());
            strJson = new String(binaryData, StandardCharsets.UTF_8);
        } catch (IOException e) {
            e.printStackTrace();
        }
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Ricky Vene
  • 105
  • 1
  • 11
  • Hi do you know how to create a file while my spring application is in the docker container? If you can answer my question, thanks!! – Sonn Aug 28 '22 at 07:54
  • You can do this from [Baeldung](https://www.baeldung.com/java-write-byte-array-file) – Ricky Vene Aug 28 '22 at 18:45