简体   繁体   中英

Null results when reading a resource

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>

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.

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM