Java : getResourceAsStream in static method

To call getResourceAsStream in a static method, we use ClassName.class instead of getClass()

1. In non static method


	getClass().getClassLoader().getResourceAsStream("config.properties"))

2. In static method


	ClassName.class.class.getClassLoader().getResourceAsStream("config.properties")) 

1. Non Static Method

A .properties file in project classpath.

src/main/resources/config.properties

#config file
json.filepath = /Users/mkyong/Documents/workspace/SnakeCrawler/data/
FileHelper.java

package com.mkyong;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class FileHelper {

    public static void main(String[] args) {

        FileHelper obj = new FileHelper();
        System.out.println(obj.getFilePathToSave());

    }

    public String getFilePathToSave() {

        Properties prop = new Properties();

        String result = "";

        try (InputStream inputStream = getClass()
				.getClassLoader().getResourceAsStream("config.properties")) {
			
            prop.load(inputStream);
            result = prop.getProperty("json.filepath");

        } catch (IOException e) {
            e.printStackTrace();
        }

        return result;

    }

}

Output


/Users/mkyong/Documents/workspace/SnakeCrawler/data/

2. Static Method

If the method getFilePathToSave() is converted into 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

To fix this, update getClass() to ClassName.class

FileHelper.java

package com.mkyong;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class FileHelper {

    public static void main(String[] args) {

        System.out.println(getFilePathToSaveStatic());
    }    

    public static String getFilePathToSaveStatic() {

        Properties prop = new Properties();

        String result = "";

        try (InputStream inputStream = FileHelper.class
				.getClassLoader().getResourceAsStream("config.properties")) {
				
            prop.load(inputStream);
            result = prop.getProperty("json.filepath");

        } catch (IOException e) {
            e.printStackTrace();
        }

        return result;

    }

}

Output


/Users/mkyong/Documents/workspace/SnakeCrawler/data/

References

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
12 Comment threads
2 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
13 Comment authors
JinhuiTomSkyerSiva MunnaluriConSgoKrishna Recent comment authors
newest oldest most voted
Cuneyt
Guest
Cuneyt

Hi,

What if i use FileHelper.class.getResourceAsStream(“config.properties”) without using getClassLoader() method. Are there any differencies?

Thanks.

asu
Guest
asu

It’s very usefull.
But please tell me. Why you do not close inputStream?

Siva Munnaluri
Guest
Siva Munnaluri

Can you tell me how to mock this code in the junit

pony
Guest
pony

Useful. Tnx.

Forhad
Guest
Forhad

This is really cool. It’s just save my time . Specially calling frrom static method.
Exactly what i am looking for.

Jinhui
Guest
Jinhui

There’re two “class”s inside “ClassName.class.class.getClassLoader().getResourceAsStream(“config.properties”))” at the beginning of the post. Guess there should only be one, right?

Denís Navarro Alcaide
Guest
Denís Navarro Alcaide

thanks! works perfectly with static methods

Harshad Loya
Guest
Harshad Loya

My getResourceAsStream is returning me null value… can you tell why it would be so?

ConSgo
Guest
ConSgo

mine too

TomSkyer
Guest
TomSkyer

Use it without getClassLoader()

oussama
Guest
oussama

i am using getResourceAsStream to read an image but when i export my runnable jar i can not accessed to it . so i wonder if there is a solution , just a notification it’s work on eclipse .

Hassan Ali
Guest
Hassan Ali

Nice explanation.

trackback
Java Properties file examples

[…] Java : GetResourceAsStream In Static Method […]

Krishna
Guest
Krishna

Thanks.. It was useful..