Where to put resources?

2019-03-14 23:20发布

I need to add some properties file to my application. I have added this file to controller directory, but can't load them (no in classpath ?) - InputStream is null. Where to put this file to can be accessed ?

public class Application extends Controller {

    static {
        try {
            Properties p = new Properties();
            InputStream in =  Application.class.getClassLoader().getResourceAsStream("accounts.properties");
            if(in != null) {
                p.load(in);
                in.close();
            } else {
                error("null inputstream");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }       
    }

    // Actions below 
    // ...
}

1条回答
闹够了就滚
2楼-- · 2019-03-14 23:46

You have to put it in the conf folder of your Play app.

You can also use subfolder in the conf directory.

For instance:

conf/foo/bar.txt

can be accessed using:

InputStream in = MyClass.class. getResourceAsStream("/foo/bar.txt")

You can also add a custom resources directory in your app, by updating your project/Build.scala file and adding:

val main = play.Project(appName, appVersion, appDependencies).settings(
      ...
      resourceDirectory in Compile <<= baseDirectory / "myresources"
      ...
  )
查看更多
登录 后发表回答