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
562 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,500 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,620 points
1,211 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 25, 2018 in Java by Rishabh
• 3,620 points
926 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
2,219 views
0 votes
1 answer

How to run a .java file from command line passing arguments in Maven?

Hello @kartik, You could run: mvn exec:exec -Dexec.args="arg1". This will ...READ MORE

answered Jun 3, 2020 in Java by Niroj
• 82,840 points
21,219 views
0 votes
1 answer

Difference between JSF, Servlet and JSP?

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

answered Jul 3, 2018 in Java by samarth295
• 2,220 points
1,563 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,190 points
3,845 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
5,447 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,220 points
3,973 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,500 points
2,983 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,500 points
16,868 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP