我希望我的应用程序独立于操作系统。因此,我的 config.properties 和日志文件存储在资源文件夹中,我通过相对路径获取这些资源。这是我的项目结构。
这是我的 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>
我使用 Intellij Idea 并运行 maven package命令,其结果是下一个输出:
[INFO] 正在扫描项目... [INFO] -------------------------------------------------- ---------------------------------- [INFO] 未命名的建筑物 - 聊天:服务器聊天:jar:1.0 [信息]
任务段:[包] [INFO] ---------------------------------------- -------------------------------- [INFO] [resources:resources {execution: default-resources}] [WARNING]使用平台编码(实际上是UTF-8)来复制过滤的资源,即构建依赖于平台![INFO] 复制 2 个资源 [INFO] [compiler:compile {execution: default-compile}] [INFO] 无需编译 - 所有类都是最新的 [INFO] [resources:testResources {execution: default-testResources}] [警告] 使用平台编码(实际上是 UTF-8)复制过滤的资源,即构建依赖于平台![INFO] 跳过不存在的资源目录 /home/nikolay/IdeaProjects/Chat/src/test/resources [INFO] [compiler:testCompile {execution: default-testCompile}] [INFO] 没有要编译的东西 - 所有类都是最新的 [信息] [肯定:-------------------------------------------------- ----- 测试 -------------------------------------------- ----------- 运行 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 [INFO] -------- -------------------------------------------------- -------------- [INFO] 构建成功 [INFO] ---------------------------- -------------------------------------------------------- [INFO] 总时间: 11 秒 [INFO] 完成时间:Mon Sep 08 09:47:18
EEST 2014 [INFO] 最终内存:18M/154M [INFO]
一开始,我为运行应用程序的服务器部分创建了 Server-Chat jar,然后将 artifactId 更改为 Client-Chat 并清单 mainClass 以创建应用程序的客户端部分。我在终端输入命令中运行的两个部分分别为: java -jar Server-Chat-1.0.jar或java -jar Client-Chat-1.0.jar。
这是服务器的输出:
java.io.FileNotFoundException: java.io.FileInputStream.open(Native Method) 处的 config.properties (没有这样的文件或目录)
和客户:
eaProjects/Chat/target $ java -jar Client-Chat-1.0.jar java.io.FileNotFoundException: config.properties (没有这样的文件或目录)