2016-04-26 60 views
1

我在資源文件夾下的java項目中有資源。當我使用下面的方法[2]來加載它正在工作的資源。但是當我在野蠻9.x部署我的戰爭時,它說找不到file.avsc文件。 它給出類路徑爲[1];我如何加載jboss war中的資源文件?如何在jboss war中加載資源文件 - wildfly9.xV

[1]

java.io.FileNotFoundException:/content/ratha.war/WEB-INF/lib/core-0.0.1-SNAPSHOT.jar/avro_schemas/file.avsc(沒有這樣的文件或目錄)

[2]

ClassLoader classLoader = getClass().getClassLoader(); 

    ClassLoader classLoader = this.getClass().getClassLoader(); 

    ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 

    File file = new File(classLoader.getResource("avro_schemas/file.avsc").getFile()); 

回答

1

嘗試Class.getResourceAsStream()方法:

this.getClass().getResourceAsStream("avro_schemas/file.avsc"); 

您可能不得不修補一下路徑。以下是關於如何構建路徑的正式文檔:Class.getResourceAsStream

問題將出現在Jboss如何創建ClassLoader結構。您將需要構建路徑來匹配類在ClassLoader類路徑中的表示方式。此

其他好的描述可以在這裏找到:How to read a file from jar in Java? 這裏:How can I read a resource file in an unexploded war file deployed in Tomcat?

凡建議你應該有一個前導「/」開始的文件路徑。

+0

謝謝你的作品.. – Ratha