4

I am new to play framework. I want to know how to use the property file in play framework.

My property file is,

conf/test.properties
name=kumar
framework=playframework

so now i want to use test.properties inside my controller class (Application.java).

Please let me know what are the steps i need to do.

Johny T Koshy
  • 3,857
  • 2
  • 23
  • 40
Sai
  • 1,075
  • 5
  • 31
  • 58

2 Answers2

4

All files placed in the conf/ folder are automatically added to your classpath. You should be able to access the your file like this:

Properties prop;
try {
  prop = new Properties();
  InputStream is = this.getClass().getResourceAsStream("test.properties");
  prop.load(is);
} catch (FileNotFoundException e) {
  e.printStackTrace();
} catch (IOException e) {
  e.printStackTrace();
}

String name = prop.getProperty("name");
String framework = prop.getProperty("playframework");

Note: I haven't tested any of the above.

Update:

I've just realized that this question is a close duplicate of Load file from '/conf' directory on Cloudbees but since my solution also includes how to access the properties in the file, I'll leave it as is.

Also since your controller method will most likely be static the above might fail to compile. In the answer I referenced they suggested using the facility provided by Play!:

Play.application().resourceAsStream("test.properties")
Community
  • 1
  • 1
jsonmurphy
  • 1,600
  • 1
  • 11
  • 19
-1

you can access to property files in play framework by doing:

val ConfigLoader = play.Play.application.configuration
  implicit val connector = ContactPoints(Seq(ConfigLoader.getString("cassandra.host"))).keySpace(ConfigLoader.getString("cassandra.keyspace"))
Alex Riabov
  • 8,655
  • 5
  • 47
  • 48
thor-tech
  • 62
  • 3
  • sorry, in your example is it not clear how name of the file is mentioned. The OP has a file called test.properties. Where are you using that in your example? – Manu Chadha Jul 17 '20 at 05:51