Java : getResourceAsStream in static method

A property file in project classpath.

config.properties

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

1. Non Static Method

Normally, you can access above properties file via getClass().getClassLoader().getResourceAsStream.


package com.mkyong.crawler.util;

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

public class FileHelper {
	
  public String getFilePathToSave() {

	Properties prop = new Properties();
	String filePath = "";

	try {

	  InputStream inputStream = 
	    getClass().getClassLoader().getResourceAsStream("config.properties");
				
	  prop.load(inputStream);
	  filePath = prop.getProperty("json.filepath");
		
        } catch (IOException e) {
		e.printStackTrace();
	}

	return filePath;

  }

}

Run it.


FileHelper helper = new FileHelper();		
String filePath = helper.getFilePathToSave();

2. Static Method

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


package com.mkyong.crawler.util;

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

public class FileHelper {
	
  public static String getFilePathToSave() {

	Properties prop = new Properties();
	String filePath = "";

	try {

	  InputStream inputStream = 
	    FileHelper.class.getClassLoader().getResourceAsStream("config.properties");

	  prop.load(inputStream);
	  filePath = prop.getProperty("json.filepath");
		
	} catch (IOException e) {
		e.printStackTrace();
	}

	return filePath;

  }

}
	
String filePath = FileHelper.getFilePathToSave();

References

  1. Dzone – Loading A Property File From The Classpath
  2. Java ClassLoader JavaDoc

About the Author

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

Comments

Leave a Reply

avatar
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.

Siva Munnaluri
Guest
Siva Munnaluri

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

asu
Guest
asu

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

Krishna
Guest
Krishna

Thanks.. It was useful..

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()

pony
Guest
pony

Useful. Tnx.

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.

Forhad
Guest
Forhad

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

trackback
Java Properties file examples

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