Java getClass().getClassLoader().getResource(路径)在Maven shaded.jar中失败

Java getClass().getClassLoader().getResource(路径)在Maven shaded.jar中失败,java,maven,jar,maven-shade-plugin,maven-jar-plugin,Java,Maven,Jar,Maven Shade Plugin,Maven Jar Plugin,当直接使用java运行我的项目时,我可以确认特定方法能够通过getClass().getClassLoader().getResource(path)获取资源,其中path是docs/info.md,并且src/main/resources/docs/info.md 在项目与Maven打包到最终的可执行文件.jar中之后,我可以通过jar tf myproject.jar确认路径docs/info.md已经包含在内 但是,当执行该.jar时,相同的getClass().getClassLoade

当直接使用
java
运行我的项目时,我可以确认特定方法能够通过getClass().getClassLoader().getResource(path)获取资源,其中path
docs/info.md
,并且
src/main/resources/docs/info.md

在项目与Maven打包到最终的可执行文件.jar中之后,我可以通过
jar tf myproject.jar
确认路径
docs/info.md
已经包含在内

但是,当执行该.jar时,相同的getClass().getClassLoader().getResource(path)返回null

这里是myproject.pom

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
         xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>

  <!-- io.outright.myproject [parent] -->
  <parent>
    <groupId>io.outright</groupId>
    <artifactId>myproject</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>

  <!-- io.outright.myproject -->
  <groupId>io.outright.myproject</groupId>
  <artifactId>hub</artifactId>
  <version>1.0-SNAPSHOT</version>
  <name>hub</name>

  <!-- props -->
  <properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <!-- deps -->
  <dependencies>

     <!-- ....removed for readability.... -->

  </dependencies>

  <!-- build -->

  <build>

    <resources>
      <resource>
        <directory>${basedir}/src/main/resources</directory>
        <includes>
          <include>**/*</include>
        </includes>
      </resource>
    </resources>

    <plugins>

      <!-- Integration tests -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <version>2.19</version>
        <executions>
          <execution>
            <id>integration-test</id>
            <phase>integration-test</phase>
            <goals>
              <goal>integration-test</goal>
            </goals>
          </execution>
          <execution>
            <id>verify</id>
            <phase>verify</phase>
            <goals>
              <goal>verify</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.5.1</version>
        <inherited>true</inherited>
        <configuration>
          <source>${maven.compiler.source}</source>
          <target>${maven.compiler.target}</target>
        </configuration>
      </plugin>

      <plugin>
        <!-- Build an executable JAR -->
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.0.2</version>
        <configuration>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <!--<classpathPrefix>lib/</classpathPrefix>-->
              <mainClass>io.outright.myproject.Main</mainClass>
            </manifest>
          </archive>
        </configuration>
      </plugin>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.4.3</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <artifactSet>
                <excludes>
                  <exclude>junit:junit</exclude>
                  <!--<exclude>log4j:log4j:jar:</exclude>-->
                </excludes>
              </artifactSet>
            </configuration>
          </execution>
        </executions>
      </plugin>

    </plugins>
  </build>

</project>

4.0.0
完全
我的项目
1.0-快照
io.outlett.myproject
中心
1.0-快照
中心
1.8
1.8
UTF-8
${basedir}/src/main/resources
**/*
org.apache.maven.plugins
maven故障保护插件
2.19
集成测试
集成测试
集成测试
验证
验证
验证
maven编译器插件
2.5.1
真的
${maven.compiler.source}
${maven.compiler.target}
org.apache.maven.plugins
maven jar插件
3.0.2
真的
io.outline.myproject.Main
org.apache.maven.plugins
maven阴影插件
2.4.3
包裹
阴凉处
junit:junit

使用
getResourceAsStream
而不是
getResource
getResource
返回文件的URL,而不是文件本身。然后将
InputStream
写入一个文件,并使用该文件

ClassLoader classLoader = getClass().getClassLoader();
if (classLoader.getResourceAsStream(filePath) == null) {
    // throw error
}
InputStream inputStream = classLoader.getResourceAsStream(filePath);
// write input stream to a file

我也面临同样的问题。这可能会有所帮助:

使用
getResourceAsStream
而不是
getResource
getResource
返回文件的URL,而不是文件本身。然后将
InputStream
写入一个文件,并使用该文件

ClassLoader classLoader = getClass().getClassLoader();
if (classLoader.getResourceAsStream(filePath) == null) {
    // throw error
}
InputStream inputStream = classLoader.getResourceAsStream(filePath);
// write input stream to a file

我也面临同样的问题。这可能会有帮助:

看到这个答案可能:不确定这是否是问题所在,但我们通常会在资源路径的开头使用“/”。所以:…getResource(“/docs/info.md”)。@Rob no。在类上调用getResource时使用前导/,否则路径是相对于类包的搜索。在类加载器上调用时,路径不能以/开头。事实证明,根据@RC comment和stackoverflow.com/a/43672609/180100,我的解决方案是使用(而不是类加载器)getClass().getResourceAsStream(path);。此外,根据@Rob comment(除了^^^),我在资源路径的开头使用了前导“/”。也许可以看到这个答案:不确定这是否是问题所在,但我们通常会在资源路径的开头使用“/”。所以:…getResource(“/docs/info.md”)。@Rob no。在类上调用getResource时使用前导/,否则路径是相对于类包的搜索。在类加载器上调用时,路径不能以/开头。事实证明,根据@RC comment和stackoverflow.com/a/43672609/180100,我的解决方案是使用(而不是类加载器)getClass().getResourceAsStream(path);。此外,根据@Rob注释(除了^^^),我在资源路径的开头使用了前导“/”。+1
getResourceAsStream
就是它。这个答案非常接近于可以接受的正确答案。。。但是,我想编辑掉
mavenProject
引用和文件编写。原始问题假设该文件是手动包含在
src/main/resources
中的,这将使正确答案略短(并且比这个答案更专有)@charneykaye删除了不必要的
mavenProject
引用和文件编写。+1
getresourceastream
是吗。这个答案非常接近于可以接受的正确答案。。。但是,我想编辑掉
mavenProject
引用和文件编写。原始问题假设该文件是手动包含在
src/main/resources
中的,这将使正确答案略短(并且比这个答案更专有)@charneykaye删除了不必要的
mavenProject
引用和文件编写。