读取资源时结果为空 [英] Null results when reading a resource

查看:76
本文介绍了读取资源时结果为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的项目结构:

这是我的代码,试图读取资源文件夹中的文件:

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"));
  }

结果都是空的:

null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null

将configuration.properties"替换为src/main/resources/configuration.properties"(带斜杠和不带斜杠)没有任何区别.

Replacing "configuration.properties" with "src/main/resources/configuration.properties" (both with slash and without slash) doesn't make any difference.

其他答案,例如 this,告诉使用 .getClass().getClassLoader().getResource(fileName) 但这已经是其中之一.为什么它们都是空的,我如何获取资源?

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>

推荐答案

找到了答案.出于某种原因,Eclipse 将排除模式**"在 main/java/resources 文件夹中.如果其他人有同样的问题:右键单击项目->构建路径 ->配置构建路径 ->源选项卡.对于所有条目,请检查排除"语音,它应该是(无)".如果您有一个排除模式从类路径中排除您的文件(如**"),请单击删除"将其删除.

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.

出于某种原因,Eclipse 会在您运行 Maven 时将 ** 的排除模式添加到 src/main/resource 文件夹 ->更新项目

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

编辑 2: 我发现 Eclipse 中 src/main/resource 文件夹上的排除模式是正常的(请参阅 this 答案).排除意味着它不是 Eclipse 处理 src/main/resources 文件夹编译,而是 Maven(准确地说是 Eclipse 的 Maven 插件,M2Eclipse).在类路径中找不到这些资源的事实是由于 pom.xml 中存在排除:

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

如果要从 jar 中排除资源,请在 maven-jar-plugin 部分中将其排除.如果要在输出中生成从 jar 中排除的资源,请使用指定目标文件夹的目标 copy-resources 配置 maven-resources-plugin(默认情况下,资源被打包到工件中,如果您只是将它们从神器,你将无处获得资源).

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

这篇关于读取资源时结果为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆