0

I saw How do I load a file from resource folder? and that is for that is programmatically getting the resource and I don't face any issue when I run the code locally.

I am facing a weird issue. I have a Runner file which looks at the files present at - src/main/resources. Runner works fine if I run it standalone, but when I make a jar and try to run the jar it could not locate the files in src/main/resources.

Jar Structure- made by Springboot

extent-confif.xml is present in the jar

Project Structure

When I run

java -jar Java_Project.jar

then I get error:

Jul 05, 2022 10:17:17 AM com.aventstack.extentreports.config.external.XmlConfigLoader createStream             
SEVERE: src\main\resources\extent-config.xml                                                                   
java.io.FileNotFoundException: src\main\resources\extent-config.xml (The system cannot find the path specified)

Build section of POM.xml looks like

<build>
  <resources>
    <resource>
      <directory>${basedir}/src/main/resources</directory>
    </resource>
  </resources>
  <plugins>

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>${maven.surefire.version}</version>
      <executions>
        <execution>
          <id>default</id>
          <goals>
            <goal>test</goal>
          </goals>
          <phase>test</phase>
        </execution>
      </executions>
      <configuration>
        <includes>
          <include></include>
        </includes>
      </configuration>
      <dependencies>
        <dependency>
          <groupId>org.apache.maven.surefire</groupId>
          <artifactId>surefire-junit47</artifactId>
          <version>${maven.surefire.version}</version>
        </dependency>
      </dependencies>
    </plugin>
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
      <executions>
        <execution>
          <goals>
            <goal>repackage</goal>
          </goals>
          <configuration>
            <mainClass>
              Runner
            </mainClass>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

Any solution or pointer is appreciated.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Abhi
  • 309
  • 1
  • 10
  • When you package your application in a JAR file, resource files, such as `extent-config.xml` need to be in the JAR file and not in folder `src\main\resources`. – Abra Jul 06 '22 at 13:33
  • @Abra : Yes correct. That's why if you see the second screenshot extent-config.xml can be see in BOOT-INF\classes folder. Did I put it at wrong place? if you see POM.xml I tried to add resources in build section also – Abhi Jul 06 '22 at 14:15
  • I don't know if you put it in the wrong place but the path to the file, in the JAR, cannot be `src\main\resources` if it is in `classes` subfolder of `BOOT-INF`. Does this help? [Spring Boot access static resources missing scr/main/resources](https://stackoverflow.com/questions/36371748/spring-boot-access-static-resources-missing-scr-main-resources) – Abra Jul 06 '22 at 14:25
  • You should not resolve resource as files, nor should you resolve them as `src/main/resources`, when resources are packaged, the contents of `src/main/resources` is put in the classpath root (so `src/main/resources/extent-config.xml` should be loaded with `someClassOrClassloader.getResourceAsStream("/extent-config.xml")`, or one of the mechanism of Spring to load resources. You should **not** use file system access, because those files are inside the JAR, not on the file system. – Mark Rotteveel Jul 06 '22 at 16:45

0 Answers0