Loading a properties file from Java package
When loading the Properties from a Class in the package com.al.common.email.templates
you can use
Properties prop = new Properties();
InputStream in = getClass().getResourceAsStream("foo.properties");
prop.load(in);
in.close();
(Add all the necessary exception handling).
If your class is not in that package, you need to aquire the InputStream slightly differently:
InputStream in =
getClass().getResourceAsStream("/com/al/common/email/templates/foo.properties");
Relative paths (those without a leading '/') in getResource()
/getResourceAsStream()
mean that the resource will be searched relative to the directory which represents the package the class is in.
Using java.lang.String.class.getResource("foo.txt")
would search for the (inexistent) file /java/lang/String/foo.txt
on the classpath.
Using an absolute path (one that starts with '/') means that the current package is ignored.
How to open a properties file from another package on Netbeans (JAVA)
You can Use Resource Bundle for that.
ResourceBundle resBundle = ResourceBundle.getBundle("PropertyFileName"); // without extention
String name= resBundle.getString("Required Attribute"); // example username
Reading Properties file in Java
Based on your exception, the InputStream
is null, this means the class loader is not finding your properties file. I'm guessing that myProp.properties is in the root of your project, if that's the case, you need a preceding slash:
InputStream stream = loader.getResourceAsStream("/myProp.properties");
How to read data from java properties file using Spring Boot
You can use @PropertySource
to externalize your configuration to a properties file. There is number of way to do get properties:
1.
Assign the property values to fields by using @Value
with PropertySourcesPlaceholderConfigurer
to resolve ${}
in @Value
:
@Configuration
@PropertySource("file:config.properties")
public class ApplicationConfiguration {
@Value("${gMapReportUrl}")
private String gMapReportUrl;
@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
return new PropertySourcesPlaceholderConfigurer();
}
}
2.
Get the property values by using Environment
:
@Configuration
@PropertySource("file:config.properties")
public class ApplicationConfiguration {
@Autowired
private Environment env;
public void foo() {
env.getProperty("gMapReportUrl");
}
}
Hope this can help
Java load properties file correctly
Use following :
InputStream input = Main.class.getResourceAsStream("../../../../config/settings.properties");
Properties prop = System.getProperties();
prop.load(input);
By adding ../
I am getting 1 step backward in your filesystem. In your code you were using /config.....
, which means C:/config.....
(if it is in C drive).
The following will also work in your case. (but not in zipped file)
InputStream input = new FileInputStream("config/settings.properties");
Properties prop = System.getProperties();
prop.load(input);
Load Properties file in java
You can load your properties file in java class in this way:-
InputStream fileStream = new FileInputStream(configPropertiesFile);
currConfigProperties.load(fileStream);
Also, change your property file path to src/com/abc/applet/Configuration.properties
Also you can use this to load it from the classpath:-
currConfigProperties.load(this.getClass().getResourceAsStream(configPropertiesFile));
Related Topics
How to Install Intellij Idea on Ubuntu
How to Downsample Images Within PDF File
Start a Jar File Like Service in Linux
Error: Unable to Load Installed Packages Just Now
How to Make Rjava Use the Newer Version of Java on Osx
Convert Existing Eclipse Project to Maven Project
Initializing Multiple Variables to the Same Value in Java
Calculating Distance Between Two Points, Using Latitude Longitude
Upload Files from Java Client to a Http Server
Java Embedded Databases Comparison
How to Connect to Ftps Server with Data Connection Using Same Tls Session
Expected Begin_Array But Was Begin_Object at Line 1 Column 2
Exception in Thread "Main" Java.Lang.Noclassdeffounderror: Org/Openqa/Selenium/Webdriver
How to Add a Shortcut Key for a Jbutton in Java
Android: How to Stretch an Image to the Screen Width While Maintaining Aspect Ratio