0

This is my project structure:

enter image description here

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>

Luke
  • 1,633
  • 3
  • 23
  • 37
  • Can you tell us where `configuration.properties` actually is located in the JAR/WAR which you are running? My guess is that it isn't there, since one of your many calls is probably the one you should be using. – Tim Biegeleisen Mar 16 '18 at 11:23
  • `Application.class.getClassLoader().getResource("configuration.properties")` should work, try rebuilding the project and check the classpath set in the launching configuration is correct (following maven conventions) - assuming you are running it from Eclipse. – Alexandre Dupriez Mar 16 '18 at 11:25
  • Can you show use your POM? – Nyamiou The Galeanthrope Mar 16 '18 at 11:27
  • I'm not running it from jar, I'm running it from Eclipse. I rebuilt the project but didn't make any difference. These are the screenshots of the launch configuration: https://imgur.com/a/mD5cj https://imgur.com/a/rlMKB. I put the pom in the question. – Luke Mar 16 '18 at 11:44

2 Answers2

0

You have to provide the full path to the class loader in case you want to load it from the project directories.

System.out.println(Application.class.getResourceAsStream("/main/resources/configuration.properties"));

Hope this helps.

Chirag
  • 555
  • 1
  • 5
  • 20
0

Found the answer. For some reason Eclipse puts an exclusion pattern of "**" on the main/java/resources folder. If someone else has the same problem: right click on the project -> Build Path -> Configure Build Path -> Source tab. For all entries check the "Excluded" voice, it should be "(None)". If you have an exlcusion pattern that excludes your files from the classpath (like "**") click on Remove to remove it.

EDIT: for some reason Eclipse adds an exclusion pattern of ** to the src/main/resource folder when you run Maven -> Update project

EDIT 2: I found that the exclusion pattern in Eclipse on src/main/resource folder is normal (see this answer). The exclusion means that it's not Eclipse handling the src/main/resources folder compilation but it's Maven (the Maven plugin of Eclipse to be precise, M2Eclipse). The fact that those resources weren't found in the classpath was due to the exclusion present in the pom.xml:

<resource>
    <directory>src/main/resources</directory>
    <excludes>
        <!-- these resources will be excluded from the classpath; they will not go in to the target/classes folder and will not be packaged into the artifact -->
        <exclude>**/*</exclude>
    </excludes>
</resource>

which I removed to get the output listed below.

Now the output of the code above is this:

java.io.BufferedInputStream@7852e922
file:/C:/Users/Taiano/eclipse-workspace/sharedProjects/passgen/target/classes/configuration.properties
null
null
java.io.BufferedInputStream@4e25154f
file:/C:/Users/Taiano/eclipse-workspace/sharedProjects/passgen/target/classes/configuration.properties
null
null
null
null
java.io.BufferedInputStream@70dea4e
file:/C:/Users/Taiano/eclipse-workspace/sharedProjects/passgen/target/classes/configuration.properties
null
null
java.io.BufferedInputStream@5c647e05
file:/C:/Users/Taiano/eclipse-workspace/sharedProjects/passgen/target/classes/configuration.properties

If you want to exclude a resource from the jar exclude it in the maven-jar-plugin section. If you want to produce in output the resources that you excluded from the jar, configure the maven-resources-plugin with the goal copy-resources specifing the destination folder (by default resources are packaged into the artifact, if you just exclude them from the artifact you will have resources nowhere).

Luke
  • 1,633
  • 3
  • 23
  • 37