I am using this example to read from configuration file (data such as host name, password, etc) . But they did not include the Configurations class itself.
So I am not really sure how that should be implemented.
Here is how I am trying to read the properties from Main class:
Configurations configs = new Configurations(); // Error: cannot find symbol symbol: class Configurations location: class Main
try {
Configuration config = configs.properties(new File("database.properties"));
String dbHost = config.getString("database.host");
int dbPort = config.getInt("database.port");
String dbUser = config.getString("database.user");
String dbPassword = config.getString("database.password", "secret"); // provide a default
long dbTimeout = config.getLong("database.timeout");
} catch (ConfigurationException cex) {
cex.printStackTrace();
}
And this is how my database.properties file looks:
database.host = "dbname";
datatabase.port = 5005;
datatabase.user = "root";
datatabase.password = "";
database.timeout = 60000
P.S. Sorry for my stupidity, I am very new to Java.
You can use the properties class in java, which has a load method that specifies an inputstream.
Then, you can read your properties file via FileInputStream.
example:
public class Test {
public static void main(String[] args) throws Exception {
Properties properties = new Properties();
InputStream inputStream =
new FileInputStream("D:\\work_space\\java_workspace\\test-mq\\src\\main\\resources\\database.properties");
properties.load(inputStream);
String host = properties.getProperty("database.host");
// get more properties......
System.out.println(host);
}
}
I dont really understand why but I get file not found when I try to load the properties.
I put a file called c24.properties in the resources folder alongside with application.properties
private static final String PROP = "c24.properties";
private Properties properties;
// Constructor
public KTServices() {
try {
properties = new Properties();
properties.load(new FileInputStream(PROP)); // FILE NOT FOUND??
} catch (IOException e) {
e.printStackTrace();
}
}
Any help? Why it doesn't find? This is a Spring Boot maven project
PS: I just checked the /target and the c24.properties is in the root folder as expected
Properties appProps = new Properties();
try {
appProps.load(Thread.currentThread().getContextClassLoader().getResource("app.properties").openStream());
} catch (IOException var3) {
this.log.error("properties file not found", var3.toString());
}
return appProps;
If the file is in the same directory as the class file, you can use the following:
properties.load(KTServices.class.getResourceAsStream(PROP));
Any property files that are outside the jar requires a full path to locate successfully.
How to read this (fixedRate = 12000) 12000 form property file in Spring.
#Scheduled(fixedRate=120000)
public void tlogZipping() throws MposWSException {
LOGGER.info("Started tlog Zipping Job............. {}" + new Date());
try {
//......................
} catch (Exception e) {
LOGGER.error("FAIL TO CREATE RECEIPT ZIP FILE: {}",e);
throw new MposWSException(MposWSErrorCodes.FAIL_TO_CREATE_RECEIPT_ZIP_FILE, e);
}
LOGGER.info("Stopped tlog Zipping Job.............");
}
You can add your properties file to the folder where your classes are exists.
and then try this code.
#PropertySource("classpath:config.properties") //set your Properties file source.
public class YourClass{
//1.2.3.4
#Value("${TLOG_ZIPPING_TIME_INTERVEL_IN_MINUTES }") //read your Property Key
private String IntervalTimeInMin; //Store in this Variable.
//hello
#Value("${anotherProperty}") //readd another Property Key
private String anotherProperty; //Store in this Variable.
For more assistence you can refer this Link Here
I´ve included a jar (I built it: MyJar.jar) that i use to authenticate, in that jar I have to read some properties that I have in a package (e.g com.myproject.properties) inside a property file of my project.
I have a method in MyJar.jar which receive the name of the property and the path for the property file something like this:
public String getValueOfProperty(String property,String pathToPropertyFile){
BasicTextEncryptor encryptor = new BasicTextEncryptor();
Properties props = new EncryptableProperties(encryptor);
props.load(new FileInputStream(pathToPropertyFile));
....
}
I got the following error: System can't find the specified path
The path is: com/myproject/properties/conf.properties
What am I doing wrong?
Solved,
This is what i Used:
The path the method receives is something like this: /sv/com/myproject/configuration/file.properties,
This method will search with relative path
public String decryptVar(String var,String PathPropertyFile) throws Exception{
try {
BasicTextEncryptor encryptor = new BasicTextEncryptor();
Properties props = new EncryptableProperties(encryptor);
InputStream fis = this.getClass().getResourceAsStream(PathPropertyFile);
props.load(fis);
encryptor.setPassword(props.getProperty("PROPERTY_IN_FILE"));
return props.getProperty(var);
} catch (Exception e) {
throw new Exception(e.getMessage());
}
}
Implementing: JASYPTH
I need to read a properties files that's buried in my package structure in com.al.common.email.templates.
I've tried everything and I can't figure it out.
In the end, my code will be running in a servlet container, but I don't want to depend on the container for anything. I write JUnit test cases and it needs to work in both.
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.
To add to Joachim Sauer's answer, if you ever need to do this in a static context, you can do something like the following:
static {
Properties prop = new Properties();
InputStream in = CurrentClassName.class.getResourceAsStream("foo.properties");
prop.load(in);
in.close()
}
(Exception handling elided, as before.)
The following two cases relate to loading a properties file from an example class named TestLoadProperties.
Case 1: Loading the properties file using ClassLoader
InputStream inputStream = TestLoadProperties.class.getClassLoader()
.getResourceAsStream("A.config");
properties.load(inputStream);
In this case the properties file must be in the root/src directory for successful loading.
Case 2: Loading the properties file without using ClassLoader
InputStream inputStream = getClass().getResourceAsStream("A.config");
properties.load(inputStream);
In this case the properties file must be in the same directory as the TestLoadProperties.class file for successful loading.
Note: TestLoadProperties.java and TestLoadProperties.class are two different files. The former, .java file, is usually found in a project's src/ directory, while the latter, .class file, is usually found in its bin/ directory.
public class Test{
static {
loadProperties();
}
static Properties prop;
private static void loadProperties() {
prop = new Properties();
InputStream in = Test.class
.getResourceAsStream("test.properties");
try {
prop.load(in);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public class ReadPropertyDemo {
public static void main(String[] args) {
Properties properties = new Properties();
try {
properties.load(new FileInputStream(
"com/technicalkeeda/demo/application.properties"));
System.out.println("Domain :- " + properties.getProperty("domain"));
System.out.println("Website Age :- "
+ properties.getProperty("website_age"));
System.out.println("Founder :- " + properties.getProperty("founder"));
// Display all the values in the form of key value
for (String key : properties.stringPropertyNames()) {
String value = properties.getProperty(key);
System.out.println("Key:- " + key + "Value:- " + value);
}
} catch (IOException e) {
System.out.println("Exception Occurred" + e.getMessage());
}
}
}
Assuming your using the Properties class, via its load method, and I guess you are using the ClassLoader getResourceAsStream to get the input stream.
How are you passing in the name, it seems it should be in this form: /com/al/common/email/templates/foo.properties
I managed to solve this issue with this call
Properties props = PropertiesUtil.loadProperties("whatever.properties");
Extra, you have to put your whatever.properties file in /src/main/resources
Nobody mentions the similar but even simpler solution than above with no need to deal with the package of the class. Assuming myfile.properties is in the classpath.
Properties properties = new Properties();
InputStream in = ClassLoader.getSystemResourceAsStream("myfile.properties");
properties.load(in);
in.close();
Enjoy
use the below code please :
Properties p = new Properties();
StringBuffer path = new StringBuffer("com/al/common/email/templates/");
path.append("foo.properties");
InputStream fs = getClass().getClassLoader()
.getResourceAsStream(path.toString());
if(fs == null){
System.err.println("Unable to load the properties file");
}
else{
try{
p.load(fs);
}
catch (IOException e) {
e.printStackTrace();
}
}