Я пытаюсь настроить отчет об экстенте, используя файл config.xml. Все отлично работает при запуске из eclipse. У меня есть config.xml внутри папки ресурсов. Однако, когда я экспортирую как исполняемый JAR-файл, код дает сбой с NPE, так как не может найти файл config.xml. Я пробовал различные варианты, а также пробовал писать во временный файл, как показано ниже.
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);}
В приведенном выше коде временный файл создается, но не имеет содержимого. Любая помощь высоко ценится
Это работает для меня, но не идеально. 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);
Поскольку других ответов нет, обновление обходного пути, который я использовал в качестве ответа
Строковый ресурс = "extent-config.xml";
InputStream input = MyClassName.class.getResourceAsStream ("/resources/" + ресурс); Тело строки = IOUtils.toString(input, StandardCharsets.UTF_8.name());
extensiontempFile = File.createTempFile("extent-config", ".xml", новый файл("./")); extensiontempFile.deleteOnExit();
try (FileOutputStream out = new FileOutputStream(extenttempFile)) {IOUtils.write(body, out);}
отчет.loadConfig(extenttempFile);
Ответ @MForm в ссылке помог разобраться