0

How can I load properties file located under src/main/resources/utility from classpath?

2 Answers 2

1
Properties prop = new Properties(); try { // load a properties file prop.load(new FileInputStream("src/main/resources/utility/config.properties")); // get the property value and print it out System.out.println(prop.getProperty("database")); System.out.println(prop.getProperty("dbuser")); System.out.println(prop.getProperty("dbpassword")); } catch (IOException ex) { ex.printStackTrace(); } 

Example retrieved from http://www.mkyong.com/java/java-properties-file-examples/

Sign up to request clarification or add additional context in comments.

2 Comments

It is usually a bad idea to reference the src/ folder but at compile time, never at runtime. Rather use utility/config.properties, as this matches the runtime classpath.
Infact #luanjot, you mislead the section, was section 3 with properties file from classpath, more correct way isprop.load(App.class.getClassLoader().getResourceAsStream("config.properties")));, however thank you very much!
0

getResourceAsStream will locate the files relative to the "root" of the classpath

initFile - File Location:

init file can also be packed as a part of war.

Properties props = new Properties(); InputStream uin = this.getClass().getResourceAsStream(initFile); InputStreamReader isr = new InputStreamReader(uin); BufferedReader in = new BufferedReader(new InputStreamReader(uin)); props.load(uin); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.