0

リソース ディレクトリ (src/java/resources) を持つ Maven ベースの Java プロジェクトを作成しました。その中にいくつかの xml ファイルがあり、maven はそれらを target/classes/resources にコピーします (target/classes は私のクラスパスです)。

xml ファイルの内容を知るには、次を使用します。

new FileInputStream(Main.class.getClassLoader().getResource("Configuration.xml").getPath());

Main.class.getClassLoader().getResource("Configuration.xml").getPath() は、xml のフル パス「c:\project...\Configuration.xml」を提供します。

これは intellij でうまく機能します。

しかし、プロジェクトをコンパイルしてjarファイルにパッケージ化すると、次のようになります。

Exception in thread "main" java.io.FileNotFoundException: 
file:\C:\project\target\project-12.50.14-SNAPSHOT-jar-with-dependencies
.jar!\Configuration.xml 

(The filename, directory name, or volume label syntax is incorrect)

これは、パス: \C:\project\target\project-12.50.14-SNAPSHOT-jar-with-dependencies .jar!\Configuration.xml を FileInputStream() のパラメーターにすることができないためです。

getResource() を getResourceAsStream() に置き換えることはできません。

jar とリソースを intellij で動作するように調整する方法が必要です。また、実行時にxmlリソースファイルのコンテンツを変更するため、jar内に保持することの別の問題です。

コードを変更する必要がなく、代わりにリソースのディレクトリ構造を変更したり、クラスパスを変更したり、コードが機能し続けるようなソリューションを提供してくれる人はいますか? ありがとう。

4

1 に答える 1