1

It seems that in my Tapestry app, I can't load ini files nor properties file from WEB-INF directory or class path.

I tried several different methods which should load my file but non of them worked.

  1. ex

realm.setResourcePath("/WEB-INF/auth.properties");

  1. ex

realm.setResourcePath("classpath:wip/pages/auth.properties");

I need to load properties/ini file in order to use tapestry-security module which is based on Shiro.

Thanks for help !

Jean-Rémy Revy
  • 5,431
  • 3
  • 36
  • 64
xyz
  • 2,197
  • 2
  • 23
  • 41

4 Answers4

4

Try ServletContext.getResourceAsStream("/WEB-INF/auth.properties") or ServletContext.getResourceAsStream("WEB-INF/auth.properties")

ServletContext has to be use from servlet, servletListener etc.

Andrzej Jozwik
  • 13,093
  • 3
  • 56
  • 65
  • It doesn't work :( I'm checking it by: `InputStream in = getClass().getResourceAsStream("classpath:wip/pages/auth.properties"); System.out.println(in);` `ServletContext.getResourceAsStream()` doesn't work either. – xyz Mar 12 '12 at 08:44
  • I fouund old post: [Howto load a resource from WEB-INF directory of a web archive](http://stackoverflow.com/q/1108434/651140) – Andrzej Jozwik Mar 12 '12 at 09:08
2

The root of the classpath is the way to go. Put your file in src/main/resources/auth.properties then set your resourcePath using realm.setResourcePath("classpath:auth.properties");

Check the ExtendedPropertiesRealm and the tapestry-security testapp for an example

ascandroli
  • 3,239
  • 1
  • 12
  • 15
  • Great mate, thanks it worked like a charm ^^ Still i don't know why it didn't work from WEB-INF :( – xyz Mar 12 '12 at 09:54
0

Try

Properties props = new Properties();
props.load(new FileInputStream(new File(req.getServletContext().getRealPath("/WEB-INF/fileName.properties"))));
System.out.println(props);
David
  • 6,663
  • 4
  • 26
  • 45
0

I found the easiest way was to

  • put file in src/main/resources/config.properties. This will be put in /WEB-INF/classes/config.properties when the project is compiled by maven into WAR

  • read the file from a servlet with the following

    InputStreaminputStream = getClass().getClassLoader().getResourceAsStream("config.properties");

https://crunchify.com/java-properties-file-how-to-read-config-properties-values-in-java/

Colin D
  • 1,857
  • 1
  • 26
  • 29