0

Am trying to customize extent report using config.xml file. All works fine when running from eclipse. I have my config.xml inside resources folder. However, when I export as runnable JAR file, the code fails with NPE as it couldnt' find the config.xml file. I have tried various options and also tried writing to temp file as shown below.

         ClassLoader classLoader = ClassLoader.getSystemClassLoader();
         InputStream resource = classLoader.getClass().getResourceAsStream(extentfileName);
         final File tempFile = File.createTempFile("extent-config", ".xml", new File("./"));
         System.out.println(tempFile.getAbsolutePath());
         tempFile.deleteOnExit();
         try (FileOutputStream out = new FileOutputStream(tempFile)) {
             IOUtils.copy(resource, out);}

In the above code, tempfile is created but has no contents. any help is highly appreciated

  • @MForm answer in [link](https://stackoverflow.com/questions/20389255/reading-a-resource-file-from-within-jar) helped to figure it out – karthik110885 Dec 10 '20 at 13:51
  • This works for me but not perfect. `String resource = "extent-config.xml"; InputStream input = MyClassName.class.getResourceAsStream("/resources/" + resource); String body = IOUtils.toString(input, StandardCharsets.UTF_8.name()); extenttempFile = File.createTempFile("extent-config", ".xml", new File("./")); extenttempFile.deleteOnExit(); try (FileOutputStream out = new FileOutputStream(extenttempFile)) { IOUtils.write(body, out);} report.loadConfig(extenttempFile);` – karthik110885 Dec 10 '20 at 13:53

1 Answers1

0

Since there are no other answers, updating the workaround I used as answer

String resource = "extent-config.xml"; InputStream input = MyClassName.class.getResourceAsStream("/resources/" + resource); String body = IOUtils.toString(input, StandardCharsets.UTF_8.name());
extenttempFile = File.createTempFile("extent-config", ".xml", new File("./")); extenttempFile.deleteOnExit();
try (FileOutputStream out = new FileOutputStream(extenttempFile)) {IOUtils.write(body, out);} report.loadConfig(extenttempFile);