This is my project structure:
This is my code, trying to read the file in the resources folder:
package passgen;
public class Application {
public static void main(String[] args) {
System.out.println(Application.class.getResourceAsStream("/configuration.properties"));
System.out.println(Application.class.getResource("/configuration.properties"));
System.out.println(Application.class.getClassLoader().getResourceAsStream("/configuration.properties"));
System.out.println(Application.class.getClassLoader().getResource("/configuration.properties"));
System.out.println(new Application().getClass().getResourceAsStream("/configuration.properties"));
System.out.println(new Application().getClass().getResource("/configuration.properties"));
System.out.println(new Application().getClass().getClassLoader().getResourceAsStream("/configuration.properties"));
System.out.println(new Application().getClass().getClassLoader().getResource("/configuration.properties"));
System.out.println(Application.class.getResourceAsStream("configuration.properties"));
System.out.println(Application.class.getResource("configuration.properties"));
System.out.println(Application.class.getClassLoader().getResourceAsStream("configuration.properties"));
System.out.println(Application.class.getClassLoader().getResource("configuration.properties"));
System.out.println(new Application().getClass().getResourceAsStream("configuration.properties"));
System.out.println(new Application().getClass().getResource("configuration.properties"));
System.out.println(new Application().getClass().getClassLoader().getResourceAsStream("configuration.properties"));
System.out.println(new Application().getClass().getClassLoader().getResource("configuration.properties"));
}
The results are all null:
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
Replacing "configuration.properties" with "src/main/resources/configuration.properties" (both with slash and without slash) doesn't make any difference.
Other answers, like this, tell to use .getClass().getClassLoader().getResource(fileName)
but this is already one of the lines. Why are they all null and how do I get the resource?
POM:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>passgen</groupId>
<artifactId>passgen</artifactId>
<version>1</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<build>
<finalName>passgen</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<targetPath>${project.build.directory}/dist</targetPath>
<includes>
<include>**/*<!-- all resources that go to folder, rest will go into the jar --></include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>**/*<!-- all resources that go to folder, rest will go into the jar --></exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<outputDirectory>${project.build.directory}/dist</outputDirectory>
<archive>
<manifest>
<!-- <addClasspath>true</addClasspath> -->
<mainClass>
passgen.Application
</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>