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

52,328

Solution 1

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.

Solution 2

Even if it seems simple, here is the scala way to get properties from configuration file :

Play 2.0 and 2.1 :

import play.api.Play.current
...
Play.application.configuration.getString("your.key")

Play 2.2 and +

import play.api.Play.current
...
current.configuration.getString("your.key")

Using Typesafe config

import com.typesafe.config.ConfigFactory
...
ConfigFactory.load().getString("your.key");

Solution 3

From Play 2.4 and + it is better to use dependency injection to access Configurations:

import play.Configuration;
import javax.inject.Inject;


@Inject
private Configuration configuration;

...

String value = configuration.getString("your.key");

Solution 4

Since Play 2 uses the Typesafe config library, I accessed my vars in application.conf like this :

ConfigFactory.load().getString("my.var");

Solution 5

In the play java is:

import play.Play;
...
Play.application().configuration().getString("key")
Share:
52,328
Nitzan Tomer
Author by

Nitzan Tomer

fugazi.io fugazi.io is a web based terminal application for executing local and remote commands which uses user-defined commands syntax. I've been working on it for a while now as a side project, and we're looking for help and feedback, so feel free to contact us (terminal.fugazi.io AT gmail.com). Check out the github repo. Also: software engineer web developer geek aikidoka kendoka yogi juggler (retired) skateboarder (occasional) snowboarder (24/7) smart-ass

Updated on July 08, 2022

Comments

  • Nitzan Tomer
    Nitzan Tomer almost 2 years

    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.

  • Nitzan Tomer
    Nitzan Tomer almost 12 years
    Thanks, I'm using java though and not scala
  • Cyril N.
    Cyril N. almost 12 years
    Play.application() doesn't exist anymore :/
  • ndeverge
    ndeverge almost 12 years
    Be careful with the import, do you use play.Play and not play.api.Play ?
  • nightograph
    nightograph almost 12 years
    as Cyril N mentioned Play.application() does not exist!
  • Nasir
    Nasir almost 12 years
    @nightograph can you elaborate? I just created a brand new 2.0 java project, added the following lines 'String key = Play.application().configuration().getString("key"); return ok(index.render(key));' and added a key in conf file. I get the text value in the conf file.
  • biesior
    biesior almost 12 years
    my Play.application().configuration().getString("key") is still available too. @nightograph and Cyril, could you explain ?
  • nightograph
    nightograph almost 12 years
    @MarcusbiesiorBiesioroff It was my mistake, I had play.api.Play imported instead of play.Play! sorry for the confusion
  • biesior
    biesior almost 12 years
    @nightograph, that's good news I was afraid that's something worse :) be careful, every class starting with play.api is for using in Scala controllers.
  • nightograph
    nightograph almost 12 years
    @MarcusbiesiorBiesioroff ohh!!! that's great tip Marcus. thanks so much! I was so confused why the framework would have 2 Play objects. Better naming would had been nicer Play.scala and play.java for example
  • Nasir
    Nasir almost 12 years
    I modified the answer, so hopefully, it will not cause confusion in the future :) - Kudos to nico_ekito who clarified it first
  • cdmckay
    cdmckay over 10 years
    Why can't you use current?
  • Jeroen Kransen
    Jeroen Kransen about 10 years
    Play.current.configuration.getString("your.key") seems to be the way in Play 2.2.2
  • vdshb
    vdshb almost 10 years
    Yep! This variant is way better than others. It works in filters too (before Global object initialized).
  • redent84
    redent84 over 9 years
    Beware that this will load the default configuration file, but if you loaded other files afterwards (for example, application.dev.conf) in Global, it won't be taken into account. Found out the hard way :(
  • gabby
    gabby over 9 years
    i was looking for that +1
  • Ihor Patychenko
    Ihor Patychenko about 8 years
    for exactly what I was looking for +1
  • Lasitha Yapa
    Lasitha Yapa over 7 years
    Play.application() is now deprecated. So update to the answer would be nice.
  • Nasir
    Nasir over 7 years
    @LasithaYapa I would check i.am.michiel's reply below and see if it helps. I haven't used Play in years now.
  • Cid
    Cid over 7 years
    If you are using latest version this may not work. Check Saeed's response
  • pcejrowski
    pcejrowski about 7 years
    This approach is deprecated, use dependency injection instead.
  • Karlth
    Karlth almost 7 years
    Still works in 2.5. I have no idea why a single config read suddenly needs dependency injection. :/
  • Spenhouet
    Spenhouet over 6 years
    Since Play 2.6.0 Configuration is deprecated and you need to inject com.typesafe.config.Config instead.
  • Spenhouet
    Spenhouet over 6 years
    Since 2.6.0 com.typesafe.config.Config should be used via dependency injection.
  • user3504158
    user3504158 about 5 years
    Starting from version 2.5 this approach is deprecated. Please use play.Application class which should be injected and then application.config().getString("your.property.here")