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

JavaConfigurationPlayframeworkPlayframework 2.0

Java Problem Overview


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.

Java Solutions


Solution 1 - Java

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 - Java

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 - Java

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 - Java

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

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

Solution 5 - Java

In the play java is:

import play.Play;
...
Play.application().configuration().getString("key")

Solution 6 - Java

Use as following (Tested in Play 1.2.5)

${play.configuration.getProperty('my.var')}

where my.var should be specified in application.conf file

Solution 7 - Java

As a reference to access it from the template (for play < 2)

play.configuration['your.key']

Solution 8 - Java

As folks have mentioned, Play.application.configuration no longer exists.

In Play Scala 2.3.x, to read a value from conf/application.conf, you can do the following:

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

Solution 9 - Java

In Play 1.2.x

import play.Play;
...


String version  = Play.configuration.getProperty("application.version.number", "1.1.1");

where the second parameter is the default value

Solution 10 - Java

Import this

import com.typesafe.config.Config;

and write the below lines

private Config config;
this.config = ConfigProvider.config();
String value = this.config.getString("fieldFromConfigFile");

Solution 11 - Java

import play.Play; String myVal = Play.configuration.getProperty("your.key").toString();

i use this in my app and it works

Dont forget to import play.Play. Hope it'll gives you help

Solution 12 - Java

Starting from version 2.5 please use play.Application class which should be injected and then application.config().getString("your.property.here")

Solution 13 - Java

For Java Playframework:

In Application.conf, you can put something like that:

> email="[email protected]"

some class:

> import play.Play; > > String email = Play.application().configuration().getString("key") // key ->email

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNitzan TomerView Question on Stackoverflow
Solution 1 - JavaNasirView Answer on Stackoverflow
Solution 2 - Javai.am.michielView Answer on Stackoverflow
Solution 3 - JavaSaeed ZarinfamView Answer on Stackoverflow
Solution 4 - JavaCyril N.View Answer on Stackoverflow
Solution 5 - JavaAlfavilleView Answer on Stackoverflow
Solution 6 - JavaRajeshView Answer on Stackoverflow
Solution 7 - JavafmsfView Answer on Stackoverflow
Solution 8 - JavacmdView Answer on Stackoverflow
Solution 9 - Javauser9869932View Answer on Stackoverflow
Solution 10 - JavaAtul ChavanView Answer on Stackoverflow
Solution 11 - JavaElang Putra SartikaView Answer on Stackoverflow
Solution 12 - Javauser3504158View Answer on Stackoverflow
Solution 13 - JavaAnthony PuitizaView Answer on Stackoverflow