[英]FileNotFoundException starting jar - can't see file in resources folder
我希望我的應用程序獨立於操作系統。 因此,我的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>
我使用Intellij Idea並運行maven package命令,其結果是下一個輸出:
[INFO]正在掃描項目... [INFO] -------------------------------------- ---------------------------------- [INFO]建築物未命名-chat:Server-Chat:jar:1.0 [信息]
任務段:[程序包] [信息] ---------------------------------------- -------------------------------- [INFO] [resources:resources {execution:default-resources}] [警告]使用平台編碼(實際上是UTF-8)來復制過濾的資源,即構建依賴於平台! [INFO]復制2個資源[INFO] [compiler:compile {execution:default-compile}] [INFO]無需編譯-所有類都是最新的[INFO] [resources:testResources {execution:default-testResources}] [警告]使用平台編碼(實際上是UTF-8)來復制過濾的資源,即構建依賴於平台! [INFO]跳過不存在的resourceDirectory / home / nikolay / IdeaProjects / Chat / src / test / resources [INFO] [compiler:testCompile {執行:default-testCompile}] [INFO]無需編譯-所有類都是最新的[ INFO] [surefire:test {執行:默認測試}] [INFO] Surefire報告目錄:/ home / nikolay / IdeaProjects / Chat / target / surefire-reports-------------------------------------------------- -----測試-------------------------------------------- -----------運行AppConfigTest測試運行:2,失敗:0,錯誤:0,跳過:2,經過的時間:0.129秒
結果:
測試運行:2,失敗:0,錯誤:0,跳過:2
[INFO] [jar:jar {執行:默認jar}] [INFO]構建jar:/home/nikolay/IdeaProjects/Chat/target/Server-Chat-1.0.jar [INFO] -------- -------------------------------------------------- -------------- [INFO]成功建立[INFO] ---------------------------- -------------------------------------------- [INFO]總時間: 11秒[INFO]完成於:9月08日星期一09:47:18
2014年EEST [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(本機方法)處的config.properties(無此類文件或目錄)
和客戶:
eaProjects / Chat / target $ java -jar Client-Chat-1.0.jar java.io.FileNotFoundException:config.properties(無此類文件或目錄)
src / main / resources是具有資源文件的常規約定。 當maven構建jar / war工件時,它將src / main / resources中的所有文件/目錄添加到所得工件的classpath中 。
在運行時沒有src / main / resources可用 。
在您的情況下,您可以更新程序以讀取這些文件,而無需給出任何路徑。 像下面
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;
}
}
IOW下面的代碼允許從jar應用程序的類路徑加載文件:
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
FileInputStream input = classLoader.getResourceAsStream(<relative path of the file located in the jar app>);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.