2017-09-10 9 views
3

私はJava 8プロジェクトを持っていて、設定ファイルはリソースフォルダにあり、すべてうまくいきました。今度はJava 9に切り替え、log4j.apiの要件を追加しましたが、設定ファイルはもう見つけられません。log4j2設定ファイルがJava 9で見つかりません

module-infoファイルにロガーが設定を見つけるために何かを追加する必要がありますか?

enter image description here

+0

あなたの現在のディレクトリ構造は何ですか?java 8でリソースをマップするために使用していた方法は何ですか? SRC /メイン/ javaの - - のsrc /メイン/リソース - のsrc /メイン/ javaの/パッケージ1 - のsrc /メイン/ javaの/パッケージ2 - – nullpointer

+0

@nullpointerのsrc /メイン/ javaの/ module-info.java - src/main/resources/log4j2.xml しかし、私は2番目の質問についてはわかりません。自分自身でマッピングを実際に行ったことはなく、依存関係を追加して設定ファイルを作成しました。 – EmberTraveller

+0

質問自体のプロジェクト構造のスクリーンショット/階層を更新することはできますか?また、log4jモジュールをどのようにインポートしていますか?使用されているjar/artifactのバージョンを明記してください。 – nullpointer

答えて

4

:build.gradleファイルとしてある

structure

:として

今の

、それはこの

module project.main { 
    requires jdk.incubator.httpclient; 
    requires jackson.databind; 
    requires jackson.core; 
    requires jackson.annotations; 
    requires log4j.api; 
} 

ようなものだプロジェクト構造であります少なくともlog4j-api-2.xlog4j-core-2.xを使用することを示唆しています。 (これは私がcustom maven projectに何をしたかである)

requires log4j; // not log4j.api 

compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.9.0' 
compile group: 'org.apache.logging.log4j', name: 'log4j-slf4j-impl', version: '2.9.0' 
compile group: 'org.apache.logging.log4j', name: 'log4j-1.2-api', version: '2.9.0' 

、さらにあなたが更新しなければならないmodule-info.javaconflicting dependencies are excluded


を確認してください:好ましくは、あなたのbuild.gradleファイルにこれらを追加それはあなたと同じディレクトリ構造で動作するはずです。


ヒント:私はそれからのデバッグを開始したところです。 Why do I see a warning about "No appenders found for logger" and "Please configure log4j properly"?

This occurs when the default configuration files log4j.properties and log4j.xml can not be found and the application performs no explicit configuration. log4j uses Thread.getContextClassLoader().getResource() to locate the default configuration files and does not directly check the file system...

ClassLoader#getResource方法でデバッグポイントを配置し、ちょうど図書館で探した資源の目を離しません。

また

JDK-8142968

  • JDK internal resources, other than class files, in the standard and JDK modules can no longer be located with the ClassLoader.getResourceXXX APIs. This may impact code that relies on using these APIs to get at JDK internal properties files or other resources.

と同様ClassLoader#getResourceのJavaの-docので出向中のリリースノートに記載されているようにresources/foo/bar/log4j.propertiesなどのリソースを超えるポイントを育てる:

  • Resources in named modules are subject to the encapsulation rules specified by Module.getResourceAsStream . Additionally, and except for the special case where the resource has a name ending with ".class", this method will only find resources in packages of named modules when the package is opened unconditionally (even if the caller of this method is in the same module as the resource).
+2

log4j-api-2.9.0.jarが自動モジュールとして使用されている場合、派生名は "log4j.api"なので、質問の 'requires'指示文が正しいように見えます。 log4jはプラグ可能ですので、実装が必要です。おそらくlog4j-core-2.9.0.jarに実装されています。モジュールパスまたはクラスパスに実装をデプロイできるようにする必要があります.APIによってそのパスが特定されます。設定ファイルlog4j2.xmlについては、モジュールproject.mainの最上位ディレクトリに配置されます。それがクラスパス上にある場合にも見つかるはずです。 –

+0

@Alan私が使ったディレクティブは、intelliJの示唆通りでしたが、自動モジュールの名前がそうであれば私にも疑問がありました。 log4j.apiを試してもモジュールとして解決されません。そのような場合に手動でモジュールを作成する必要があると思いますか?そしてリソースに関連して、私の理解は、それらにアクセスする方法は内部リソースのカプセル化の面で変化し、トップレベルのディレクトリではありません。私が間違っていれば私を修正してください。 – nullpointer

+0

お返事ありがとうございました。私はmavenプロジェクトを作りましたが、すべてがうまくいきましたが、Java 9でgradleプロジェクトを作成しようとすると、実際にはエラーが出ました。 "エラー:BUG!例外でフェーズ 'クラス生成'例外がソースユニット '_BuildScript_' MODULE "はちょっと調べてみて、ちょうど最近、同じ問題を抱えている人がいることに気づいた。だから実際には何かがgradleと実際に行う。とにかくこの問題を長年固執してくれてありがとう! – EmberTraveller

関連する問題