Importing properties file from Java package

0 votes

I want to import a properties file from my com.al.common.email.templates package structure. Currently, I able to run my code in a server container but I don't want that as it creates a dependency. Since I work with JUnit test cases, can someone guide me, how can I write a code that executes with both?

Oct 15, 2018 in Java by anto.trigg4
• 3,440 points
39 views

1 answer to this question.

0 votes

In case your class is present within your com.al.common.email.templates package, you can try using the below code:

Properties myProp = new Properties();
InputStream inStr = getClass().getResourceAsStream("demo.properties");
myProp.load(in);
inStr.close(); //Along with necessary exception handling

In case your class is present within your com.al.common.email.templates package, you can try using the below code:

InputStream inStr = 
 getClass().getResourceAsStream("/com/al/common/email/templates/demo.properties");

Providing the path in getResource() will help in searching for the resource in the relative directory.

answered Oct 15, 2018 by code.reaper12
• 3,470 points

Related Questions In Java

0 votes
1 answer

How do I create a Java string from the contents of a file?

If you're looking for an alternative that ...READ MORE

answered Apr 19, 2018 in Java by Rishabh
• 3,580 points
163 views
0 votes
1 answer

How to download and save a file from Internet using Java?

public void saveUrl(final String filename, final String ...READ MORE

answered May 24, 2018 in Java by Rishabh
• 3,580 points
85 views
0 votes
1 answer

Should a class file always be inside package for importing it in jsp page?

Hey there! It is not necessary for the ...READ MORE

answered May 24, 2019 in Java by Karan
131 views
+5 votes
3 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,400 points

edited Nov 6, 2018 by Omkar 16,990 views
0 votes
1 answer

Difference between JSF, Servlet and JSP?

Servlet - it's java server side layer. JSP ...READ MORE

answered Jul 2, 2018 in Java by samarth295
• 2,190 points
390 views
0 votes
1 answer

Use Servlets with Ajax

Here, we are going to create the ...READ MORE

answered Jul 16, 2018 in Java by Akrati
• 3,150 points
1,099 views
0 votes
1 answer

doGet and doPost in Servlets

When you are using HTTP GET requests ...READ MORE

answered Jul 17, 2018 in Java by Mrunal
• 680 points
1,515 views
0 votes
1 answer

web.xml file is missing in my eclipse IDE

Very simple. You will find Deployment Descriptor ...READ MORE

answered Nov 27, 2018 in Java by Omkar
• 69,000 points
420 views
0 votes
1 answer

How to download a file from spring controllers?

@RequestMapping(value = "/files/{file_name}", method = RequestMethod.GET) public void ...READ MORE

answered Sep 4, 2018 in Java by code.reaper12
• 3,470 points
356 views
0 votes
1 answer

Retrieving query from java.sql.PreparedStatement

By using prepared statements, you can't retrieve the query ...READ MORE

answered Oct 12, 2018 in Java by code.reaper12
• 3,470 points
2,051 views