YoZH
YoZH

Reputation: 257

Accessing files in Play dev and prod mode

I'm having a trouble accessing some files in universal way across app run modes. I have folder "resources" in app root folder, which contains some crucial files. When I run in dev mode, I access them in simple manner, like:

Play.application().path().getAbsolutePath()+"/resources/file.file";

But after I package my app via dist commmand (I've modified build.sbt so that "resources" folder is copied near conf and lib folders), the code above stops working, due to this line

Play.application().path().getAbsolutePath()

now returns a path to bin folder, in which app.bat is ran from. So if in dev mode, the code above returns correct path like X:/app/resources/file.file, in prod mode it's like X:/app/bin/resources/file.file which is incorrect.

P.S. I absolutely can't put my files in conf folder and access them as a resource from a classloader because of numerous reasons which are actually not important.

So the question is simple as that: how to access these file resources in a universal manner across modes, without any hardcoding. TY in advance.

Upvotes: 1

Views: 151

Answers (1)

Steve Chaloner
Steve Chaloner

Reputation: 8202

There is a method on Application which lets you access files in the application root.

https://playframework.com/documentation/2.4.x/api/java/play/Application.html#getFile-java.lang.String-

default java.io.File getFile(java.lang.String relativePath)

Get a file relative to the application root path.

Parameters: relativePath - relative path of the file to fetch

Returns: a file instance - it is not guaranteed that the file exists

Since you already have the application, you should be able to use this method to directly access the file.

Upvotes: 1

Related Questions