java - FileNotFoundException 起始 jar - 在资源文件夹中看不到文件

标签 java jar resources filenotfoundexception platform-independent

我希望我的应用程序独立于操作系统。因此,我的 config.properties 和日志文件存储在资源文件夹中,我通过相对路径获取这些资源。 这是我的项目结构。 project structure

这是我的 AppConfig 类:

public final class AppConfig {

private static final String RELATIVE_PATH_TO_PROPERTIES = "./src/main/resources/config.properties";
public static final String RELATIVE_LOG_PATH = "./src/main/resources/err_action.log";

private static Properties props = initProperties();
public static final String HOST = props.getProperty("ip_address");

public static final int PORT = Integer.valueOf(props.getProperty("port"));
public static final int MAX_USERS = Integer.valueOf(props.getProperty("max_number_of_users"));
public static final int NUM_HISTORY_MESSAGES = Integer.valueOf(props.getProperty("last_N_messages"));

private static Properties initProperties() {
    Properties properties = null;
    try (FileInputStream input = new FileInputStream(RELATIVE_PATH_TO_PROPERTIES)) {
        properties = new Properties();
        properties.load(input);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return properties;
}
}

如您所见,我为属性和日志文件指定了相对路径。 我用 Maven 创建 jar,当我运行它时,我收到

java.io.FileNotFoundException: ./src/main/resources/err_action.log (No such file or directory)

UPD 这是我的 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>chat</groupId>
<artifactId>Client-Chat</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<properties>
    <java_version>1.8</java_version>
</properties>
<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>${java_version}</source>
                <target>${java_version}</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>nikochat.com.app.Main</mainClass>
                        <!--<mainClass>nikochat.com.app.RunClient</mainClass>-->
                    </manifest>
                </archive>
            </configuration>
        </plugin>

    </plugins>
</build>
</project>

我使用 Intellij Idea 并运行 maven package 命令,结果是下一个输出:

[INFO] Scanning for projects... [INFO] ------------------------------------------------------------------------ [INFO] Building Unnamed - chat:Server-Chat:jar:1.0 [INFO]
task-segment: [package] [INFO] ------------------------------------------------------------------------ [INFO] [resources:resources {execution: default-resources}] [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 2 resources [INFO] [compiler:compile {execution: default-compile}] [INFO] Nothing to compile - all classes are up to date [INFO] [resources:testResources {execution: default-testResources}] [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory /home/nikolay/IdeaProjects/Chat/src/test/resources [INFO] [compiler:testCompile {execution: default-testCompile}] [INFO] Nothing to compile - all classes are up to date [INFO] [surefire:test {execution: default-test}] [INFO] Surefire report directory: /home/nikolay/IdeaProjects/Chat/target/surefire-reports

------------------------------------------------------- T E S T S ------------------------------------------------------- Running AppConfigTest Tests run: 2, Failures: 0, Errors: 0, Skipped: 2, Time elapsed: 0.129 sec

Results :

Tests run: 2, Failures: 0, Errors: 0, Skipped: 2

[INFO] [jar:jar {execution: default-jar}] [INFO] Building jar: /home/nikolay/IdeaProjects/Chat/target/Server-Chat-1.0.jar [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESSFUL [INFO] ------------------------------------------------------------------------ [INFO] Total time: 11 seconds [INFO] Finished at: Mon Sep 08 09:47:18

EEST 2014 [INFO] Final Memory: 18M/154M [INFO]

一开始我为运行应用程序的服务器部分创建了 Server-Chat jar,然后我将 artifactId 更改为 Client-Chat 并 list mainClass 以创建应用程序的客户端部分。 我在终端输入命令中运行的两个部分: java -jar Server-Chat-1.0.jarjava -jar < strong>Client-Chat-1.0.jar分别。

这是服务器的输出:

java.io.FileNotFoundException: config.properties (No such file or directory) at java.io.FileInputStream.open(Native Method)

和客户:

eaProjects/Chat/target $ java -jar Client-Chat-1.0.jar java.io.FileNotFoundException: config.properties (No such file or directory)

最佳答案

src/main/resources 是拥有资源文件的 Maven 约定。当 Maven 构建 jar/war 工件时,它会将所有文件/目录从 src/main/resources 添加到生成的工件的类路径

运行时没有可用的 src/main/resources

在您的情况下,您可以更新程序以读取这些文件而无需提供任何路径。如下图

private static final String RELATIVE_PATH_TO_PROPERTIES = "config.properties";
public static final String RELATIVE_LOG_PATH = "err_action.log";

关于java - FileNotFoundException 起始 jar - 在资源文件夹中看不到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25718327/

相关文章:

java - 使用 objectify 对名称/值对象的列表属性上的数据存储实体进行排序

Java:使用 -cp 选项运行的 jar 文件中的启动画面?

java - 在 Eclipse 中读取 Assets

RESTful 资源和正交资源问题

java - 理解 JViewPorts 时遇到一些困难

java - Spring Integration 微服务可视化工具

java - 如何找到所有可用的迷宫路径?

java - 我正在尝试在 IBM Domino 服务器上使用 PDFbox,得到 NoClassDefFoundError

java - 在同一个构建文件中执行 gradle shadowjar 任务两次

html - 从 CSS 链接的强大图像 URL?