How do I access a resource file using a relative p

2019-07-03 21:37发布

How do I access a resource file relative to a Play application path (even when packaged into a distribution zip)?

This is the same problem as this, but in Play 2, which has no VirtualFile class in its api.

Thanks.

1条回答
干净又极端
2楼-- · 2019-07-03 22:20

Check out play.api.Play.getFile() and play.api.Play.getExistingFile(), which get files from the file system relative to the application directory.

A similar technique that works for me is simply to add a resource directory (or jar) to the classpath in the play start script. I can then use Play.getResourceAsStream() to load files from that directory, or specify the resource as a JVM parameter in the start script (e.g. -Dconfig.resource=/my_conf/prd.conf)

查看更多
登录 后发表回答