如何获得加载文件资源文件夹下阶SBT
问题描述:
我有一个阿卡演员配置资源下的文件,如何获得加载文件资源文件夹下阶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执行时能够加载配置文件。 任何事情我做错了。
答
这是很多人遇到的流行程序。使用getResourceAsStream
这工作正常,然后在罐子里使用。
getClass.getResourceAsStream("/remote_app.conf")
感谢您的回答。这个getResourceAsStream给出输入流'getClass.getResourceAsStream(“/ remote_app.conf”)'。这个ConfigFactory'val config_mediation = ConfigFactory.parseFile(new File(path))'不支持解析输入流。有没有什么办法可以做到这一点..解析字符串是否存在或解析读者 – Gopi