Когда я запускаю main () от IntelliJ, я получил как ожидаемый Питер, Джонсон в качестве стандартного выхода. Но когда я запускаю MVN Clean Package и пытаюсь выполнить java -jar target/example-1.0.0-snapshot-jar-with-rendencies.jar Я получаю следующее сообщение об ошибке. Почему и как я могу убедиться, что файл .properties читается? < /P>
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.(FileInputStream.java:138)
at java.io.FileInputStream.(FileInputStream.java:93)
at com.example.MyPropertiesClass.getFullName(MyPropertiesClass.java:18)
at com.example.MyMainClass.main(MyMainClass.java:9)
< /code>
mymainclass < /strong> < /p>
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());
}
}
< /code>
mypropertiesclass < /strong> < /p>
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;
}
}
< /code>
my-example.properties
name=peter
lastname=johnson
< /code>
pom.xml
4.0.0
com.example
example
1.0.0-SNAPSHOT
${project.artifactId}
My Example
1.8
1.8
UTF-8
maven-assembly-plugin
3.0.0
jar-with-dependencies
true
lib/
com.example.MyMainClass
make-assembly
package
single
< /code>
< /p>
Подробнее здесь: https://stackoverflow.com/questions/413 ... unning-jar
Файл свойств Java в jar не найден при запуске JAR ⇐ JAVA
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Maven генерирует фактический jar как файл .jar.original вместо файла .jar
Anonymous » » в форуме JAVA - 0 Ответы
- 64 Просмотры
-
Последнее сообщение Anonymous
-