1

I'm trying to access my properties file in a war. The code is working, but when i'm exporting the code into a war and use a POST (with an accepted input) using Fiddler, it cannot find the config.properties. (NullPointerException)

The input and webservice are running correctly. Just trying to figure out a way to edit my properties while using a war.

Some facts:

  • I've made a RetreivePropertiesClass. This uses:

    properties.load(this.getClass()
                        .getResourceAsStream("/WEB-INF/config.properties"));
    
  • My properties file path:

    /com.webapp/WebContent/WEB-INF/config.properties
    
  • I'm trying to use the properties data in:

    String url = RetreiveProperties.getUrl();
    String driver = RetreiveProperties.getDriver();
    String username = RetreiveProperties.getUsername();
    String password = RetreiveProperties.getPassword();
    
    // line below causes the NullPointerException
    Class.forName(driver);
    
  • Getter used:

    public static String getDriver() {
    return driver = properties.getProperty("jdbc.driver");
    }
    
  • When the war is deployed, the properties file is in:

    webapps\com.webapp1\WEB-INF\config.properties
    
  • Config.properties:

    jdbc.url = jdbc:postgresql://127.0.0.1:2222/gisdb
    jdbc.driver = org.postgresql.Driver
    jdbc.username = postgres
    jdbc.password = admin
    

I already tried to work out the examples given here and here. Keeps giving the NullPointerException because the properties file isn't loaded.

Can anyone push me in the right direction?

Cheers!

NullPointer in TomCat8

Camelaria
  • 284
  • 6
  • 23
  • What is the path of the properties file if you check inside of the packaged war file? – K Erlandsson Jun 28 '15 at 18:15
  • Ah, forgot to mention it. It's in C:\Users\myusername\Downloads\apache-tomcat-8.0.21-windows-x64\apache-tomcat-8.0.21\webapps\com.webapp1\WEB-INF – Camelaria Jun 28 '15 at 18:18

2 Answers2

4

If you are going to load the properties file from the classpath, it must be in the WEB-INF/classes directory inside of your war. (Or inside a jar inside of WEB-INF/lib ). The WEB-INF directory itself is not on the classpath.

If you make sure the file ends up as WEB-INF/classes/config.properties your above code should work if you change the getResourceAsStream call to getResourceAsStream("/config.properties")

K Erlandsson
  • 13,408
  • 6
  • 51
  • 67
  • I put my properties file in: C:\Users\myusername\Downloads\apache-tomcat-8.0.21-windows-x64\apache-tomcat-8.0.21\webapps\com.webapp1\WEB-INF\classes\com\webapp. I'm using properties.load(this.getClass().getResourceAsStream("WEB-INF/classes/com/webapp/config.properties")); to get the file, but it still doesn't work. Did I make a typo? – Camelaria Jun 28 '15 at 18:32
  • P.S. The C:\ address is the dir where my properties file is stored when I look into the war file. – Camelaria Jun 28 '15 at 18:33
  • 1
    @Camelaria If you put it in that path you need to call getResourceAsStream("/com/webapp/config.properties") – K Erlandsson Jun 28 '15 at 18:34
  • Hmm, changed the loader to /com/webapp/config.properties. Still get a NullPointerException at Class.forName(driver);. This is where I use a properties value for the first time. – Camelaria Jun 28 '15 at 18:42
  • @Camelaria please post the stack trace. Is it actually the stream you get as null or something else? – K Erlandsson Jun 28 '15 at 18:44
  • Added stacktrace from Tomcat server in question. com.webapp.ConverterClass.getDetectors(ConverterClass.java:31) is the driver value. – Camelaria Jun 28 '15 at 18:48
1

If the method is a static method, the getClass() method will be failed, and prompts “Cannot make a static reference to the non-static method getClass() from the type Object“.

Instead, you should use CurrentClass.class.getClassLoader().getResourceAsStream.

Source: here

Camelaria
  • 284
  • 6
  • 23