Accessing the application.conf properties from java class with Play! 2.0

Nitzan Tomer picture Nitzan Tomer · Mar 23, 2012 · Viewed 50.3k times · Source

I want to add an object to the Global scope, and in order to construct it I need to pass it a path to a file. I don't want to hard code the file path in the source, and so I want to get that path from the application.conf.

The problem is that I don't know how to access these properties from the java class. I tried this:

Configuration.root().getString("file.path")

But it ends with a NullPointerException.

Am I wrong in assuming that there's a global Configuration instance that I can use? Thanks.

Answer

Nasir picture Nasir · Mar 24, 2012

Try Play.application().configuration().getString("your.key")

As noted in the comment (nico_ekito), please use play.Play and not play.api.Play. play.api.Play is for scala controllers (see comment by Marcus biesior Biesioroff)

Additionally, play uses https://github.com/typesafehub/config under the hood so it can also provide some insights.