3

I am having a akka actors config file under resources,

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

I am loading the resources as below,

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)

Both works fine when i execute from main program and getting the below logs,

[INFO] [11/21/2016 21:05:02.835] [main] [Remoting] Remoting started; listening on addresses :[akka.tcp://MediationActorSystem@127.0.0.1:7070] [INFO] [11/21/2016 21:05:02.838] [main] [Remoting] Remoting now listens on addresses: [akka.tcp://MediationActorSystem@127.0.0.1:7070]

I am creating a jar using sbt test:assembly and executing the main class as below SBT:

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

java -cp <jar> <args>

Its not able to load the config file when i execute from jar. Anything am i doing wrong.

PGS
  • 1,046
  • 2
  • 17
  • 38

3 Answers3

1

This is the popular program encountered by lot of people. Use getResourceAsStream This works normally and when used inside jar.

getClass.getResourceAsStream("/remote_app.conf")
Nagarjuna Pamu
  • 14,737
  • 3
  • 22
  • 40
  • Thanks for the answer. This getResourceAsStream gives the input stream `getClass.getResourceAsStream("/remote_app.conf")` . This ConfigFactory `val config_mediation = ConfigFactory.parseFile(new File(path))` does not have support to parse input stream . Is there any way i can do this.. parse string is there or parse reader – PGS Nov 21 '16 at 16:13
1

Answer from pamu is a nice hint but did not fully work for me. You still have to convert it to an InputStream. The full solution would be:

val is = getClass.getClassLoader.getResourceAsStream("remote_app.conf")
val source = scala.io.Source.fromInputStream(is).mkString
val config = ConfigFactory.parseString(source)
kaichi
  • 101
  • 8
1

The easiest way might be to use something like lightbend conf -library: https://github.com/lightbend/config

It will by default pick the application.conf file from -resources folder.

import com.typesafe.config.ConfigFactory
val conf = ConfigFactory.load()

You can also give the load -function other file names as parameter:

val conf = ConfigFactory.load("someother.conf")

Note: no slash before filename.

Pietrotull
  • 477
  • 4
  • 9