I am not able to load the properties file into a java file. getting file not found exception. Can you please help here.
Java file location:
classes/com/my/location/for/javabased/utilities/convertor/servlet/GetProp.java
Properties file location:
classes/com/my/property/properties/Config.properties
My code:
Properties inputParams = new Properties();
FileInputStream in = new FileInputStream("classes/com/my/property/properties/Config.properties");
inputParams.load(in);
in.close();
Getting File not found exception
I prefer reading the property file in class like this
public class SomeClass{
private static Properties someProperties = new Properties();
static{
someProperties.load(SomeClass.class.getResourceAsStream("/com/my/property/properties/Config.properties"));
}
Hope this helps.
The java class File (which is used by FileInputStream) is based on the file system path, either absolute, or relative to the current working directory. Both is mostly not under full control of the running application. If your resources can be found within the classpath (which from the point of view of the running application is always the same) you should use the resource loading mechanisme of your classloader (as Vijendra Kulhade pointed out in his answer).
Related
I am trying to read a properties folder from this path with respect to the repository root:
rest/src/main/resources/cognito.properties
I have a Class CognitoData from this path: rest/src/main/java/com/bitorb/admin/webapp/security/cognito/CognitoData.java which loads the Properties folder using this code, and it runs fine:
new CognitoProperties().loadProperties("rest/src/main/resources/cognito.properties");
#Slf4j
public class CognitoProperties {
public Properties loadProperties(String fileName) {
Properties cognitoProperties = new Properties();
try {
#Cleanup
FileInputStream fileInputStream = new FileInputStream(fileName);
cognitoProperties.load(fileInputStream);
} catch (IOException e) {
log.error("Error occured. Exception message was [" + e.getMessage() + "]");
}
return cognitoProperties;
}
}
However, when I call CognitoData from a test class located in rest/src/test/java/com/bitorb/admin/webapp/security/cognito/CognitoServiceTest.java , I get this error:
[rest/src/main/resources/cognito.properties (No such file or directory)]
Can anybody shed light on why this is happening?
File directory is not actually relative in that case. You need to provide appropriate file path for this. If you are already using spring boot, then
you can change your code to:
// this will read file from the resource folder.
InputStream inputStream = getClass().getClassLoader()
.getResourceAsStream("cognito.properties");
cognitoProperties.load(inputStream);
Otherwise you need to provide the full absolute path. new CognitoProperties().loadProperties("/absolutepath/..../cognito.properties")
I don't know what you're using for testing, but I suspect that the working directory when you run tests is not the project root.
One solution is to use an absolute path instead:
/absolute/path/to/project/rest/src/main/resources/cognito.properties
Or maybe check what is the working directory during testing and see if it can be changed to the project root.
Does anyone knows how to read a file from the resource folder when the application is running on the AWS Elasticbeanstalk?
Please see code below:
Resource resource = new ClassPathResource("application.properties");
File file = resource.getFile();
Map propsMap = PropertyUtil.readProperties(file);
This is the error message:
java.io.FileNotFoundException: class path resource [application.properties] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/var/app/current/application.jar!/BOOT-INF/classes!/application.properties"
Thank you in advance.
You should probably just change your PropertyUtil to be able to read from the InputStream:
Properties properties = new Properties();
try (InputStream stream =
new ClassPathResource("application.properties").getInputStream()) {
properties.load(stream);
}
Properties class is already a Map implementation so you wouldn't need to change any other code.
I am having trouble finding out the right way to load a properties file.
The structure is : Inside src/com.training , I have my class and the properties file as well. I have to read it using the absolute path as shown in the code below to get it to work:
Properties prop = new Properties();
InputStream input = null;
input = new FileInputStream("D:/Dev/workspace/Training/src/com/training/consolemessages.properties");
prop.load(input);
System.out.print(prop.getProperty("INITIAL_MESSAGE"));
How can I use the relative path to work in this code for accessing the properties file. The properties file and the class which is accessing are both at the same level '/src/com.training'
You could put your properties files into src/resources folder, and fetch them on classpath.
You could do something like this:
ResourceBundle bundle = ResourceBundle.getBundle("resources/consolemessages");
System.out.println(bundle.getString("INITIAL_MESSAGE"));
When using ResourceBundle, Locale support is also easy to implement, should you need to have language specific properties.
I recently just start to using Android Studio for my programming study.
I've faced a problem today which is keep getting "null" when I using getResourceAsStream to read a properties file from JUNIT TEST.
I used to locate the .properties file under "src" directory when I was using Eclipse. But in Android Studio, it won't work.
Here is the code that part of a class called BeanFactory,:
private static Properties properties;
static {
properties = new Properties();
InputStream is = BeanFactory.class.getClassLoader().getResourceAsStream("bean.properties");
try {
properties.load(is);
}catch (IOException e) {
e.printStackTrace();
}
}
In Eclipse, I located the the prop file under src, and it works just fine.
But in Android Studio, I've tried put the "bean.properties" file in several different directory. Such as /src, or/src/main/java, nothing worked out.
Where should I put the prop file? Is there any configurations that I should do for this?
Placing resources into assets is not acceptable in some cases. I solved this problem by packing required resourses into special jar file and placing this jar into libs directory.Thus you still can access them via standard Java technique.
There are two issues. In general resources are stored in a jar, with the compiled .class files.
getResource and getResourceAsStream are methods of a class. The resource is searched in the jar of this class (in general). So with inheritance getClass().getResource("...") might be dangerous.
The second, more grave pitfall is, that the path is relative to the package (directory) of the class, unless you use "/...".
Another way is to use ResourceBundle instead of Properties. ResourceBundle has a PropertiesResourceBundle for *.properties.
ResourceBundle properties = ResourceBundle.getBundle("bean");
try this:
1.put your own bean.properties file in app/src/main/assets
2.change you code to:
private static Properties prop;
static {
prop = new Properties();
try {
InputStream is = IWorkApplication.getInstance().getResources().getAssets().open("bean.properties", Context.MODE_PRIVATE);
prop.load(is);
} catch (IOException e) {
e.printStackTrace();
}
PS.IWorkApplication.getInstance() is define in my own application class.
Hope to be useful
I am trying to load a properties file. The properites file is in the class path of the application.
Properties p = new Properties();
p.load(new FileInputStream("classpath:mail.properties"));
System.out.println(p.get("hi"));
Now I say classpath, because another file called x.properties is referred in an xml file like this
<property name="x">
<util:properties location="classpath:x.properties" />
</property>
I placed my mail.properties in the same folder as x.properties, but my Java program is not able to find it ? Any idea what I am missing ?
Just because some program processing that XML file likes the syntax classpath:x.properties doesn't mean that it is a universally accepted syntax in Java!
If you provide "classpath:x.properties" to a FileInputStream it will look for a file named classpath:x.properties. (Check the documentation of that particular constructor.)
Try providing the full path to that file. If the file happens to be on your class path, you could use something like
p.load(getClass().getResourceAsStream("mail.properties"));
if mail.properties is indeed on your classpath, you will have better luck loading it via a class loader:
Properties p = new Properties();
InputStream is = getClass().getClassLoader().getResourceAsStream("mail.properties");
p.load(is);