Java 列出外部类路径文件夹中的文件

Java list files in external classpath folder

我正在尝试从不在 jar 中但相对于 jar 所在位置的文件夹读取文件 运行。 fat jar 是使用 maven shade 插件构建的


阴影插件

 <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-shade-plugin</artifactId>
                    <version>3.1.1</version>
                    <configuration>
                       <transformers>
                        <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                          <manifestEntries>
                            <Main-Class>com.xx.validaor.Validator</Main-Class>
                              <Class-Path>csv_files2</Class-Path>
                            <X-Compile-Source-JDK>1.9</X-Compile-Source-JDK>
                            <X-Compile-Target-JDK>1.9</X-Compile-Target-JDK>
                          </manifestEntries>
                        </transformer>
                        <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                          <resource>META-INF/spring.handlers</resource>
                        </transformer>
                        <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                          <resource>META-INF/spring.schemas</resource>
                        </transformer>


                      </transformers>
                    </configuration>
                    <executions>
                      <execution>
                        <phase>package</phase>
                        <goals>
                          <goal>shade</goal>
                        </goals>
                      </execution>
                    </executions>
                  </plugin>

这会生成清单文件

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven 3.3.9
Build-Jdk: 9.0.1
Class-Path: csv_files2
Main-Class: com.xx.validaor.Validator
X-Compile-Source-JDK: 1.9
X-Compile-Target-JDK: 1.9

这是来自文件夹

的运行
logs/
csv_files2/
murex-validator-0.0.1-SNAPSHOT.jar

在 csv_files2 中,我尝试放置另一个 csv_files2 文件夹,其中包含一堆没有改变任何内容的文件

java -jar my-0.0.1-SNAPSHOT.jar

URL resource = getClass().getClassLoader().getResource("/csv_files2/");
        if (resource == null) {
            logger.error("1   /csv_files2/ did not work");
        }

        resource = getClass().getClassLoader().getResource("csv_files2/");
        if (resource == null) {
            logger.error("2   csv_files2/ did not work");
        }

        resource = getClass().getClassLoader().getResource("/csv_files2");
        if (resource == null) {
            logger.error("3   /csv_files2 did not work");
        }

        resource = getClass().getClassLoader().getResource("csv_files2");
        if (resource == null) {
            logger.error("4   csv_files2 did not work");
        }

        resource = getClass().getClassLoader().getResource("./csv_files2");
        if (resource == null) {
            logger.error("5   ./csv_files2 did not work");
        }
        File[] files2 = Paths.get(resource.toURI()).toFile().listFiles();

输出

17:20:18.800 [main] ERROR  ValidationService - 1   /csv_files2/ did not work
17:20:18.800 [main] ERROR  ValidationService - 2   csv_files2/ did not work
17:20:18.800 [main] ERROR  ValidationService - 3   /csv_files2 did not work
17:20:18.800 [main] ERROR  ValidationService - 4   csv_files2 did not work
17:20:18.800 [main] ERROR  ValidationService - 5   ./csv_files2 did not work

尝试像这样在阴影插件中的 csv_files2 末尾添加一个“/”

<Main-Class>com.td.validaor.Validator</Main-Class>
<Class-Path>csv_files2/</Class-Path>
<X-Compile-Source-JDK>1.9</X-Compile-Source-JDK>
<X-Compile-Target-JDK>1.9</X-Compile-Target-JDK>