-1

I try to read a file from the resources and it tells me java.io.FileNotFoundException: file:/home/simon/IdeaProjects/KTMBlockChain/build/resources/main/certificate_template.docx (No such file or directory)

Note that the file is blue and clickable. When I click on it it also opens the file so it definietly exists in the place expected.

Code:

        InputStream in = null;
        try {
            File file = new File(this.getClass().getClassLoader().getResource("certificate_template.docx").toString());

            in = new FileInputStream(file);
            IXDocReport report = XDocReportRegistry.getRegistry().
                    loadReport(in, TemplateEngineKind.Freemarker);
            Options options = Options.getTo(ConverterTypeTo.PDF).via(ConverterTypeVia.ODFDOM);
            IContext ctx = report.createContext();
            ctx.put("re_wo", pdfData.getReifen());
            /*ctx.put("to", invoice.getTo());
            ctx.put("sender", invoice.getInvoicer());
            FieldsMetadata metadata = report.createFieldsMetadata();
            ctx.put("r", invoice.getInvoiceRows());*/
            report.convert(ctx, options, new FileOutputStream("result.pdf"));

I dont know what to do anymore...

EDIT 1: Changed code, still not working, another error code but same problem

1 Answers1

0

There are

  • Disk files, class File;
  • Resource files (read-only), on the class path, possibly packed in a jar or war.

So here is you should use a resource, and Path is a generalisation of File, and all other kind of URL paths. This caused the error.

URL url = getClass().getClassLoader().getResource("certificate_template.docx");
Path path = Paths.get(url.toURI());
List<String> x = Files.readAllLines(path); // Reading a UTF-8 text.

But docx is not text, but a binary format (actually a zip format).

You either need to use a library or just handle the file as-is, like let the operating system open it.

You could use Files.readAllBytes(); reading UTF-8 will probably cause an error as the bytes are not in UTF-8 format.


After edit of question:

in = getClass().getClassLoader().getResourceAsStream("certificate_template.docx");
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • Thanks for the answer. Actually I just put the iteration in before I nearly lost it. I actually use a library which also wasnt able to find it. Original answer is edited – Simon Mair May 19 '22 at 12:17
  • `*.docx` is zipped XML; but the local path is strange, because it should be inside the `*.jar`, or not? – Martin Zeitler May 19 '22 at 12:21
  • @MartinZeitler if the application is delivered as .jar (also a zip file) it should contain a path `/certificate_template.docx` (case-sensitive). – Joop Eggen May 19 '22 at 14:22
  • @JoopEggen I can only tell that the one version has a forward slash and the other hasn't. There's still the possibility, that the file may not have been packaged, in terms of "simply not there". – Martin Zeitler May 19 '22 at 21:37
  • @MartinZeitler `getClass().getResourceAsStream("/certificate_template.docx");` for a possible relative path, here absolutized. With a ClassLoader there are only absolute paths without starting `/`. And yes, one has to check classes dir, jar or whatever the deploying does, – Joop Eggen May 20 '22 at 10:26