2016-11-21 82 views
0

我有一個阿卡演員配置資源下的文件,如何獲得加載文件資源文件夾下階SBT

src/main/resources/remote_app.conf 
src/main/scala/actors/Notification.scala 

我加載的資源如下,

1.

val configFile = getClass.getClassLoader.getResource("remote_app.conf").getFile 
val config_mediation = ConfigFactory.parseFile(new File(configFile)) 
actorSystem = ActorSystem("MediationActorSystem", config_mediation) 

2.

val path = getClass.getResource("/remote_app.conf").getFile 
val config_mediation = ConfigFactory.parseFile(new File(path)) 
actorSystem = ActorSystem("MediationActorSystem", config_mediation) 

這兩個工作正常,當我從主程序執行並獲得下面的日誌,

[INFO] [11/21/2016 21:05:02.835] [main] [Remoting] Remoting開始; 收聽地址 :[akka.tcp://[email protected]:7070] [INFO] [11/21/2016 21:05:02.838] [main] [遠程處理]遠程處理現在監聽地址: [akka.tcp://[email protected]:7070]

我使用SBT測試創建一個罐子:裝配和執行主類,如下 SBT:

resourceDirectory in Compile := baseDirectory.value /"src/main/resources" 
resourceDirectory in Test := baseDirectory.value /"src/main/resources" 

java -cp <jar> <args> 

它不當我從jar執行時能夠加載配置文件。 任何事情我做錯了。

回答

0

這是很多人遇到的流行程序。使用getResourceAsStream這工作正常,然後在罐子裏使用。

getClass.getResourceAsStream("/remote_app.conf") 
+0

感謝您的回答。這個getResourceAsStream給出輸入流'getClass.getResourceAsStream(「/ remote_app.conf」)'。這個ConfigFactory'val config_mediation = ConfigFactory.parseFile(new File(path))'不支持解析輸入流。有沒有什麼辦法可以做到這一點..解析字符串是否存在或解析讀者 – Gopi