我想在我的 jar 之外有一个配置文件。 将读取配置并且程序应做出相应的行为。 我尝试使用:
this.getClass().getResource( file_name );
但是我遇到了 pom 依赖项的问题。下面的例子说明了这个问题。
src文件结构为:
main
├── java
│ └── com
│ └── company
│ └── App.java
└── resources
└── external
└── input
└── external_file.txt
目标文件结构为:
target/
├── classes
│ └── com
│ └── company
│ └── App.class
├── resources
│ └── input
│ └── external_file.txt
│
└── test-file.jar
完整代码:
package com.company;
import java.io.*;
import java.net.URL;
/**
* Hello world!
*
*/
public class App
{
public void run() {
//the module options are extended to add the resources folder to the classpath in intelliJ
String file_name = File.separator + "input" + File.separator + "external_file.txt";
System.out.println( "file name: " + file_name );
URL propertiesFileUrl = this.getClass().getResource( file_name );
String path = propertiesFileUrl.getPath();
System.out.println( "path: " + path );
File test_file = new File( path );
try{
FileReader test_fileReader = new FileReader( test_file );
BufferedReader test_bufferedReader = new BufferedReader( test_fileReader );
String test_line = test_bufferedReader.readLine();
test_fileReader.close();
System.out.println( "First line: " + test_line );
} catch(FileNotFoundException e){
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();
}
}
public static void main( String[] args )
{
new App().run();
}
}
如果我使用以下 pom.xml,此代码可以正常工作
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.company</groupId>
<artifactId>test-file</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>test-file</name>
<url>http://maven.apache.org</url>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<archive>
<manifest>
<mainClass>com.company.App</mainClass>
</manifest>
<manifestEntries>
<Class-Path>resources/</Class-Path>
</manifestEntries>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/resources</outputDirectory>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources/external</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<excludes>
<exclude>external/</exclude>
</excludes>
</resource>
</resources>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
我可以使用 IntelliJ 执行代码,因为我已将目标/资源文件夹添加为模块设置中的依赖项。 我还可以从命令行执行 jar,输出如预期:
file name: /input/external_file.txt
path: /Users/amin/Work/testjar/resources/input/external_file.txt
First line: test text!!!
但是一旦我将以下依赖项添加到我的 pom.xml
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
<scope>compile</scope>
</dependency>
,重新导入并重建所有内容,我仍然可以从 IntelliJ 成功执行程序,但从命令行执行的 jar 将输出:
file name: /input/external_file.txt
Exception in thread "main" java.lang.NullPointerException
at com.company.App.run(App.java:19)
at com.company.App.main(App.java:39)
在我的理解范围内,这根本没有意义 注意:mysql 不是唯一的包,它会将结果更改为不工作。
最佳答案
资源是 .jar 文件中的一个条目。您不能将其称为文件,因为它不是单个文件。资源应该使用 getResource 或 getResourceAsStream 读取,而文件永远不应该使用这些方法读取,因为 getResource 不是用于读取文件,而是用于读取资源。
请注意,getResource 和 getResourceAsStream 的参数不是文件名。它是指示嵌入资源的相对 URL。这意味着它不能使用 File.separator - URL 在所有平台上总是使用正斜杠 ( /
)。
您尚未显示您的 test-file.jar 是否实际上包含 external_file.txt。您的标题表明 external_file.txt 实际上不在 .jar 内。如果是这种情况,您不应该全部使用 getResource,而应该使用 Files类:
try (BufferedReader reader = Files.newBufferedReader(
Paths.get(System.getProperty("user.home"),
"projects", "app", "target", "resources",
"external", "input", "external_file.txt")) {
String test_line = reader.readLine();
System.out.println( "First line: " + test_line );
}
(请注意 try-with-resources statement 自动关闭阅读器。)
但是...我不确定您是否真的希望该文件是外部的。这是放置在项目 resources
中的文件的长期惯例。构建时要打包到 .jar 文件中的目录。您是否检查过 test-file.jar 的内容以验证其中是否包含 input/external_file.txt?
如果是,您应该使用 getResourceAsStream 读取它:
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(
App.class.getResourceAsStream("/input/external_file.txt"))) {
String test_line = reader.readLine();
System.out.println( "First line: " + test_line );
}
附注:URL 的 getPath() 方法不会将 URL 转换为有效的文件名。它仅返回 URL 的路径部分,并不能保证该部分是有效的文件名。例如:文件 C:\Users\John Doe
将表示为 URL file:/C:/Users/John%20Doe
,因为 URL 中不允许使用空格字符; getPath() 方法将返回 "/C:/Users/John%20Doe"
.
关于java - 如何读取jar外部的文件//pom中mysql依赖的getResource问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51673441/