Angelina
Angelina

Reputation: 2265

Cannot access properties file in tomcat - linux

I have placed a .properties file in the webapps folder of tomcat but am not able to access it.

My servlet is in directory under webapps.

Do I have to change my java code to look into parent directory?

 private static final ResourceBundle ldap = ResourceBundle.getBundle("js_ldap");

Upvotes: 0

Views: 1014

Answers (2)

Stefan
Stefan

Reputation: 12453

Just copy the file into your classpath (src/main/resources/anything.properties), if the file is shipped with your application. Load it as usual, using ResourceBundle or Properties class.

If the file is shared across many apps, and it is changed often or server specific, then put it in a directory anywhere on your server, give the right permissions, and load it with an absoluth path using (Classloader).getResourcesAsStream(...). Dont put it into the server directory, since it might be removed if someone changes the server.

As I can see you are trying to load LDAP properties. If you are looking for a way to authenticate users, create a JNDIRealm.

Upvotes: 1

Andres
Andres

Reputation: 10717

To access the file on your application directory, you should use the following:

private static final ResourceBundle ldap = ResourceBundle.getBundle("js_ldap.properties");

Concerning the placement of the properties file, I don't think it's a good idea to put it in the webapps directory. Consider, that you as a developer won't have access to this directory on production environments.

A better solution would be to include these files on a jar, and put this jar on Tomcat's classpath. This way the properties files could be shared by many applications.

Upvotes: 2

Related Questions