Java 在 运行 jar 时找不到 jar 中的属性文件
Java properties file in jar not found when running jar
当我从 IntelliJ 得到 运行 main()
时,我预期 peter,johnson
作为标准输出的结果。但是当我 运行 mvn clean package
并尝试执行 java -jar target/example-1.0.0-SNAPSHOT-jar-with-dependencies.jar
时,我收到以下错误消息。为什么以及如何确保读取 .properties 文件?
java -jar target/example-1.0.0-SNAPSHOT-jar-with-dependencies.jar
Exception in thread "main" java.io.FileNotFoundException: file:/Users/ismar.slomic/src/ADOP/example/target/example-1.0.0-SNAPSHOT- jar-with-dependencies.jar!/my-example.properties (No such file or directory)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at com.example.MyPropertiesClass.getFullName(MyPropertiesClass.java:18)
at com.example.MyMainClass.main(MyMainClass.java:9)
MyMainClass
package com.example;
import java.io.IOException;
public class MyMainClass {
public static void main(String[] args) throws IOException {
MyPropertiesClass mpc = new MyPropertiesClass();
System.out.println(mpc.getFullName());
}
}
我的属性类
package com.example;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class MyPropertiesClass {
public String getFullName() throws IOException {
Properties properties = new Properties();
ClassLoader cl = this.getClass().getClassLoader();
String filePath = cl.getResource("my-example.properties").getFile();
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(filePath));
properties.load(bis);
String name = properties.getProperty("name");
String lastname = properties.getProperty("lastname");
return name + "," + lastname;
}
}
我的-example.properties
name=peter
lastname=johnson
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
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>com.example</groupId>
<artifactId>example</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>${project.artifactId}</name>
<description>My Example</description>
<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>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.example.MyMainClass</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
如果您的 属性 文件在您的 jar 中,那么您最好使用 getResourceAsStream
而不是 getResource
和 getFile
.
类似于:
ClassLoader cl = this.getClass().getClassLoader();
try (InputStream stream = cl.getResourceAsStream("my-example.properties")) {
properties.load(bis);
}
方法 getResource(String)
returns URL 指向 jar 中的文件。在您的情况下,类似于:file:/Users/ismar.slomic/src/ADOP/example/target/example-1.0.0-SNAPSHOT-jar-with-dependencies.jar!/my-example.properties
。当您在 URL 上调用 getFile
时,它 returns 文件(即 jar)而不是指向内部属性文件的指针。
也可以在 URL 上打开一个连接并使用返回的 JarURLConnection
。但是 getResourceAsStream
.
更容易
当我从 IntelliJ 得到 运行 main()
时,我预期 peter,johnson
作为标准输出的结果。但是当我 运行 mvn clean package
并尝试执行 java -jar target/example-1.0.0-SNAPSHOT-jar-with-dependencies.jar
时,我收到以下错误消息。为什么以及如何确保读取 .properties 文件?
java -jar target/example-1.0.0-SNAPSHOT-jar-with-dependencies.jar
Exception in thread "main" java.io.FileNotFoundException: file:/Users/ismar.slomic/src/ADOP/example/target/example-1.0.0-SNAPSHOT- jar-with-dependencies.jar!/my-example.properties (No such file or directory)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at com.example.MyPropertiesClass.getFullName(MyPropertiesClass.java:18)
at com.example.MyMainClass.main(MyMainClass.java:9)
MyMainClass
package com.example;
import java.io.IOException;
public class MyMainClass {
public static void main(String[] args) throws IOException {
MyPropertiesClass mpc = new MyPropertiesClass();
System.out.println(mpc.getFullName());
}
}
我的属性类
package com.example;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class MyPropertiesClass {
public String getFullName() throws IOException {
Properties properties = new Properties();
ClassLoader cl = this.getClass().getClassLoader();
String filePath = cl.getResource("my-example.properties").getFile();
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(filePath));
properties.load(bis);
String name = properties.getProperty("name");
String lastname = properties.getProperty("lastname");
return name + "," + lastname;
}
}
我的-example.properties
name=peter
lastname=johnson
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
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>com.example</groupId>
<artifactId>example</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>${project.artifactId}</name>
<description>My Example</description>
<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>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.example.MyMainClass</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
如果您的 属性 文件在您的 jar 中,那么您最好使用 getResourceAsStream
而不是 getResource
和 getFile
.
类似于:
ClassLoader cl = this.getClass().getClassLoader();
try (InputStream stream = cl.getResourceAsStream("my-example.properties")) {
properties.load(bis);
}
方法 getResource(String)
returns URL 指向 jar 中的文件。在您的情况下,类似于:file:/Users/ismar.slomic/src/ADOP/example/target/example-1.0.0-SNAPSHOT-jar-with-dependencies.jar!/my-example.properties
。当您在 URL 上调用 getFile
时,它 returns 文件(即 jar)而不是指向内部属性文件的指针。
也可以在 URL 上打开一个连接并使用返回的 JarURLConnection
。但是 getResourceAsStream
.