运行jar时找不到jar中的Java属性文件

伊斯玛·斯洛米奇(Ismar Slomic)

当我main()从IntelliJ运行时,我得到了预期的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());
   }
}

MyPropertiesClass

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;
  }
}

my-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)返回指向jar内文件的URL。在您的情况下,类似:file:/Users/ismar.slomic/src/ADOP/example/target/example-1.0.0-SNAPSHOT-jar-with-dependencies.jar!/my-example.properties当您调用getFile该URL时,它将返回文件(即jar),而不是指向内部属性文件的指针。

也可以在URL上打开连接并使用返回的JarURLConnection但是使用会更容易getResourceAsStream

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

运行jar时项目抛出IOException(找不到文件)

来自分类Dev

Java JAR找不到文件

来自分类Dev

Java JAR找不到文件

来自分类Dev

找不到主类:运行Java Jar时?

来自分类Dev

Java在Linux中的.jar文件中找不到类

来自分类Dev

Java在Linux中的.jar文件中找不到类

来自分类Dev

Capistrano找不到.jar文件

来自分类Dev

jar 找不到 JSON 文件

来自分类Dev

尝试执行 JAR 文件时找不到无效的 URL 或资源

来自分类Dev

java.lang.RuntimeException:找不到流“ /jars/oozie-examples.jar”。在CDH 5.8中从oozie运行spark工作流程时

来自分类Dev

可运行的jar找不到库

来自分类Dev

运行gradle生成的jar找不到jar条目

来自分类Dev

安装OpenCV后找不到.jar文件

来自分类Dev

Maven Jar文件找不到主类

来自分类Dev

安装OpenCV后找不到.jar文件

来自分类Dev

找不到要导入的jar文件

来自分类Dev

以.jar执行专案时找不到bluecove jar

来自分类Dev

从java中的jar导入文件时出错

来自分类Dev

Java makefile 找不到 JAR 资源

来自分类Dev

Spring ResourceLoader在JAR中找不到资源

来自分类Dev

在 Maven 存储库中找不到 Jar

来自分类Dev

从jar文件中填充属性文件时出错

来自分类Dev

在Ubuntu上运行.jar时出错:找不到ot加载主类

来自分类Dev

Eclipse 在尝试导出到可运行的 JAR 时找不到主要方法

来自分类Dev

VScode找不到JAR

来自分类Dev

使找不到工具.jar

来自分类Dev

Blackberry错误:无法运行程序“ jar”:CreateProcess错误= 2,系统找不到指定的文件

来自分类Dev

使用包含的jar文件从Apache Ant运行JUnit,结果为“找不到JUnitTask”

来自分类Dev

Java jar文件未运行