1

Whenever I try to load a properties file by below method. I get an error on getClass() as-

Cannot make a static reference to the non-static method getClass() from the type Object

public static void main(String[] args) {

        ---
        ---

    loadProperties(line);

}

private static void loadProperties(String line) {
        Properties prop = new Properties();
        InputStream in = getClass().getResourceAsStream("foo.properties");
        try {
            prop.load(in);
            for(Object str: prop.keySet()) {
                Object value = prop.getProperty((String) str);
                System.out.println(str+" - "+value);
            }
            in.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

Any suggestions how can I overcome this.

skaffman
  • 398,947
  • 96
  • 818
  • 769
arsenal
  • 23,366
  • 85
  • 225
  • 331

4 Answers4

4

You can't call getClass from a static method. You either need to do this in a non-static method:

class MyClass {
   public static void main(String[] args) {
      MyClass obj = new MyClass();
      obj.loadProperties(line);    
   }

   private void loadProperties(String line) {
        Properties prop = new Properties();
        InputStream in = getClass().getResourceAsStream("foo.properties");
        ...  
   }
}

or use a class literal, which does work in a static context, e.g.

InputStream in = MyClass.class.getResourceAsStream("foo.properties");
skaffman
  • 398,947
  • 96
  • 818
  • 769
  • 1
    Note that if you use `getClass()` instead of a class literal, funny things can happen in the presence of subclasses (you might get a different package than you thought you would). – Thilo Nov 21 '11 at 06:00
2

To access properties file which is in project class path from static method usign getClass try like :

import java.io.InputStream;
import java.util.Properties;
import java.io.IOException;
public class HelperClass {
     public static String getProperty() {
        Properties properties = new Properties();
        String wadlURI = "";
           try {
               InputStream inputStream = HelperClass.class.getClassLoader().getResourceAsStream("configuration.properties");
                properties.load(inputStream);
               wadlURI = properties.getProperty("wadlurl");
           } catch (IOException e) {
               e.printStackTrace();
             }
          return wadlURI;
      }
}
Asraful
  • 1,241
  • 18
  • 31
0

I was facing the same issue in IntelliJ IDEA and was following the same step. But i didn't mark the resource folder as "Resource Root". After doing it problem resolved for me.

0

To access property file and load it to application you can use the following.

public static void loadPropertiesToMemory() {
    Properties prop = new Properties();
    InputStream input = null;
    String filePathPropFile = "configuration.properties";
    try {
        input = App.class.getClassLoader().getResourceAsStream(filePathPropFile);
        prop.load(input);
    } catch (IOException ex) {
        ex.printStackTrace();
    } 
}

App is the class where this method exists.

Du-Lacoste
  • 11,530
  • 2
  • 71
  • 51