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,420 points
10 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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,450 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,520 points
59 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,520 points
26 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,360 points

edited Nov 6, 2018 by Omkar 4,430 views
0 votes
4 answers

Remove extra spaces from string in java

import java.util.regex.Matcher; import java.util.regex.Pattern; String pattern="[\\s]"; String replace=""; part="name=john age=13 year=2001"; Pattern ...READ MORE

answered Sep 10, 2018 in Java by parth295
• 4,590 points
49 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
190 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
198 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
600 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
• 65,820 points
29 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,450 points
67 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,450 points
82 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.