2

アプリケーションを OS に依存しないようにしたいと考えています。したがって、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] 建物名無し - chat:Server-Chat:jar:1.0 [情報]
タスク セグメント: [パッケージ] [情報] ---------------------------------------- -------------------------------- [INFO] [resources:resources {execution: default-resources}] [WARNING]プラットフォーム エンコーディング (実際には UTF-8) を使用してフィルター処理されたリソースをコピーします。つまり、ビルドはプラットフォームに依存します! [INFO] 2 つのリソースをコピーしています [INFO] [compiler:compile {execution: default-compile}] [INFO] コンパイルするものはありません - すべてのクラスは最新です [INFO] [resources:testResources {execution: default-testResources}] [ WARNING] プラットフォーム エンコーディング (実際には UTF-8) を使用してフィルタリングされたリソースをコピーします。つまり、ビルドはプラットフォームに依存します! [INFO] 存在しない resourceDirectory /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] Building jar: /home/nikolay/IdeaProjects/Chat/target/Server-Chat-1.0.jar [INFO] -------- -------------------------------------------------- -------------- [情報] ビルド成功 [情報] ---------------------------- -------------------------------------------- [情報] 合計時間: 11 秒 [INFO] 終了時刻: Mon Sep 08 09:47:18

EEST 2014 [情報] 最終記憶: 18M/154M [情報]

最初に、アプリケーションのサーバー部分を実行するための 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 (そのようなファイルまたはディレクトリはありません)

4

2 に答える 2

4

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";
于 2014-09-08T06:21:16.577 に答える
3

以下のコードは、ビルド後に jar ファイルのルート ディレクトリにデプロイされた resources フォルダーにある config.properties ファイルをロードできるようにする必要があります。

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>);
于 2015-03-13T08:14:11.253 に答える