eclipsejarselenium-extent-report

Exported JAR from Eclipse - Class file and config.xml file inside jar - How do I use this config file in xx.class


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


Solution

  • 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);