读取资源时结果为空

时间:2018-03-16 11:21:06

标签: java resources

这是我的项目结构:

enter image description here

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

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”(使用斜杠和不使用斜杠)没有任何区别。

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

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>

2 个答案:

答案 0 :(得分:0)

如果要从项目目录中加载它,则必须提供类加载器的完整路径。

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

希望这有帮助。

答案 1 :(得分:0)

找到答案。出于某种原因,Eclipse提出了排除模式&#34; **&#34;在main / java / resources文件夹中。如果其他人有同样的问题:右键单击项目 - &gt;构建路径 - &gt;配置构建路径 - &gt;来源标签。对于所有条目,请检查&#34;排除&#34;声音应该是&#34;(无)&#34;。如果您有一个excusion模式从类路径中排除您的文件(例如&#34; **&#34;),请单击“删除”将其删除。

编辑:由于某些原因,Eclipse在运行Maven时向src / main / resource文件夹添加了**的排除模式 - &gt;更新项目

编辑2:我发现Eclipse上src / main / resource文件夹中的排除模式是正常的(请参阅this回答)。排除意味着它不是Eclipse处理src / main / resources文件夹编译,而是它的Maven(精确的Eclipse的Maven插件,M2Eclipse)。在类路径中找不到这些资源的事实是由于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>

我删除了以获得下面列出的输出。

现在上面代码的输出是:

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(默认情况下,资源被打包到工件中,如果您只是将它们从神器你将无处可用资源)。