Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find the absolute path to a Play Framework app?

At the moment I'm working with a team on a Play! Framework app. With the next user story, I have to implement some different file modifications, such as moving a file to a defined directory.

Because we are working on different platforms, I'm not always really sure if the app has the right path. So I want to work with a absolute path of the app directory.

How do I get the absolute path of a Play! app? Is there a method for this?

like image 385
Tobias Schmid Avatar asked Oct 31 '10 18:10

Tobias Schmid


People also ask

What is play framework in Scala?

Play Framework makes it easy to build web applications with Java & Scala. Play is based on a lightweight, stateless, web-friendly architecture. Built on Akka, Play provides predictable and minimal resource consumption (CPU, memory, threads) for highly-scalable applications.

Is Play Framework popular?

In July 2015, Play was the 3rd most popular Scala library in GitHub, based on 64,562 Libraries. 21.3% of the top Scala projects used Play as their framework of choice. As of October 2013, the Play Framework is the most popular Scala project on GitHub.


3 Answers

This answer applies only to older versions of the Play Framework, before v2.

Play has an application path property:

String projectRoot = Play.applicationPath;

This will give you the directory that Play is running from.

I think a better practice is moving the directory outside of your project install directory and placing the path to it in your application.conf as a property. You then retrieve it when needed. For example:

Application.conf:

my.file.path=/tmp/whatever

Code:

String projectRoot = Play.configuration.getProperty("my.file.path");
like image 94
Damo Avatar answered Sep 24 '22 08:09

Damo


Since version 2.5.0, play.Play class is deprecated. It is recommended to inject play.Environment and use the method as follows:

public File rootPath();

The play.Environment singleton also contains some very handy method, which provide files by relative path e.g.

public File getFile(String relativePath);
public URL resource(String relativePath);
public InputStream resourceAsStream(String relativePath);
like image 31
Tom Avatar answered Sep 24 '22 08:09

Tom


As of play 2.0: play.Play.application().path().getAbsolutePath()

like image 26
user363349 Avatar answered Sep 20 '22 08:09

user363349