Java FileNotFoundException启动jar-can';在资源文件夹中看不到文件

Java FileNotFoundException启动jar-can';在资源文件夹中看不到文件,java,jar,resources,filenotfoundexception,platform-independent,Java,Jar,Resources,Filenotfoundexception,Platform Independent,我希望我的应用程序与操作系统无关。因此,我的config.properties和日志文件存储在resources文件夹中,我使用相对路径获取这些资源。 这是我的项目结构 以下是我的AppConfig类: public final class AppConfig { private static final String RELATIVE_PATH_TO_PROPERTIES = "./src/main/resources/config.properties"; public static fi

我希望我的应用程序与操作系统无关。因此,我的config.properties和日志文件存储在resources文件夹中,我使用相对路径获取这些资源。 这是我的项目结构

以下是我的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(没有这样的文件或目录)

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>

4.0.0
聊天
客户端聊天
1
罐子
1.8
朱尼特
朱尼特
4.11
org.apache.maven.plugins
maven编译器插件
3.1
${java_版本}
${java_版本}
org.apache.maven.plugins
maven jar插件
2.4
nikochat.com.app.Main
我使用Intellij Idea并运行mavenpackage命令,其结果是下一个输出:

[信息]正在扫描项目。。。[信息] ------------------------------------------------------------------------[信息]未命名的建筑-聊天室:服务器聊天室:jar:1.0[信息]
任务段:[包][信息] ------------------------------------------------------------------------[信息][资源:资源{执行:默认资源}][警告] 使用平台编码(实际上是UTF-8)复制过滤后的资源, i、 e.构建依赖于平台![信息]正在复制2个资源[信息] [编译器:编译{执行:默认编译}][INFO]无需任何内容 编译-所有类都是最新的[INFO][resources:testResources {执行:默认testResources}][WARNING]使用平台编码 (实际上是UTF-8)复制过滤后的资源,即构建是平台 依赖![信息]跳过不存在的resourceDirectory /home/nikolay/IdeaProjects/Chat/src/test/resources[信息] [编译器:testCompile{执行:默认testCompile}][INFO]无 编译-所有类都是最新的[INFO][surefire:test {执行:默认测试}][INFO]Surefire报告目录: /home/nikolay/IdeaProjects/Chat/target/surefire报告

-------------------------------------------------------T T S T S -------------------------------------------------------正在运行AppConfigTest测试运行:2,失败:0,错误:0,跳过:2,时间 已用时间:0.129秒

结果:

测试运行:2,失败:0,错误:0,跳过:2

[INFO][jar:jar{execution:default jar}][INFO]构建jar: /home/nikolay/IdeaProjects/Chat/target/Server-Chat-1.0.jar[信息] ------------------------------------------------------------------------[信息]构建成功[信息] ------------------------------------------------------------------------[信息]总时间:11秒[信息]完成时间:周一至九月08日09:47:18

EEST 2014[信息]最终内存:18M/154M[信息] 在开始时,我为运行应用程序的服务器部分创建服务器聊天jar,然后将artifactId更改为Client Chat,并将manifest main类更改为创建应用程序的客户端部分。 我在终端键入命令中运行的两部分: java-jar服务器聊天-1.0.jarjava-jar客户端聊天-1.0.jar

以下是服务器的输出:

java.io.FileNotFoundException:config.properties(没有这样的文件或目录) 在java.io.FileInputStream.open(本机方法)

客户:

eaProjects/Chat/target$java-jar Client-Chat-1.0.jar java.io.FileNotFoundException:config.properties(没有这样的文件或目录)


src/main/resources是maven拥有资源文件的惯例。当maven构建jar/war工件时,它将src/main/resources中的所有文件/目录添加到生成工件的类路径中

运行时没有可用的src/main/资源

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

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

下面的代码应该允许将位于resources文件夹中的config.properties文件加载到jar文件的根目录中,该文件夹在构建之后部署:

public final class AppConfig {

private static final String RELATIVE_PATH_TO_PROPERTIES="config.properties";
...
private static Properties initProperties() {
    Properties properties = null;

    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    FileInputStream input =    classLoader.getResourceAsStream(RELATIVE_PATH_TO_PROPERTIES);
    try {
        properties = new Properties();
        properties.load(input);
    } catch (IOException e) {
        e.printStackTrace();
    }
    ...
    return properties;
    }
}
下面的代码允许从jar应用程序的类路径加载文件:

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
FileInputStream input = classLoader.getResourceAsStream(<relative path of the file located in the jar app>);
ClassLoader ClassLoader=Thread.currentThread().getContextClassLoader();
FileInputStream输入=classLoader.getResourceAsStream();

您是否使用maven创建jar?请记住,当您说相对路径时,它将相对于类文件而不是源文件。是的,我使用的是maven。无需指定这些路径;Maven将
src/main/resources
中的任何ia复制到jar中的主文件夹中,但是,该路径中的一个文件在类路径中直接可用,因此您可以在不指定任何文件夹的情况下加载它。这些文件夹结构只适用于源代码组织,但不一定是最终jar的文件夹结构。我试着按照您所说的做,但仍然收到java.io.FileNotFoundException:config.properties(没有这样的文件或目录)也许我需要从jar读取文件?我查看了jar的结构、属性和放置在根文件夹“/”中的日志文件。我将config类中的路径更改为:/config.properties/err_action.log,现在我收到FileNotFoundException:/err_action.log(权限被拒绝),也许有另一种解决方案可以创建独立于平台的应用程序来避免此权限被拒绝的错误?是的,您是对的。我在写日志文件方面遇到了一些问题,这就是为什么这个应用程序不能工作的原因。