FileNotFoundException con archivo de propiedades
Frecuentes
Visto 185 veces
0
I'm trying to read some props from backupData.properties
. It locates in WEB-INF/classes/
. This is how I do:
public class Configures {
private static final String INPUT_FILE = "WEB-INF//classes//backupData.properties";
public static String getMail() {
Properties prop = new Properties();
try {
//load a properties file
prop.load(new FileInputStream(INPUT_FILE));
//get the property value
return prop.getProperty("mail");
} catch (IOException ex) {
ex.printStackTrace();
}
return null;
}
}
Qué INPUT_FILE
should contain? I was trying to put it in src
del ADN, tales como los src//backupData.properties
, pero tira FileNotFoundException
. I googled that file should locate in CLASSPATH
(en WEB-INF/classes
as I understood). What is wrong?
PS. I'm using Spring.
2 Respuestas
2
This has nothing to do with Spring. If you are deploying a web application, everything in WEB-INF/classes
will appear starting at the root of the classpath.
You can get it the InputStream
to that resource with
InputStream in = Configures.class.getResourceAsStream("/backupData.properties");
prop.load(in);
Since a web application is not always extracted from its .war
file, the actual properties file might only exist as a zip entry. As such, you can't (and shouldn't) retrieve it with FileInputStream
.
Respondido el 28 de enero de 14 a las 18:01
I don't think you want the slash in the name. - jalynn2
@jalynn2 Why don't you think so? - Sotirios Delimanolis
because it is in the root of the classpath? - jalynn2
@jalynn2 For that specific reason, it needs to have the leading /
. Otherwise, it would be resolved relative the package that Configures
es en. - Sotirios Delimanolis
@Tony You're welcome. Take a look at the javadoc I've linked and try to familiarize yourself with the classpath. Web apps are a little different than standalone apps in that regard. - Sotirios Delimanolis
1
Since you are using SPRING , I suggest you "CAN" use this as part of your Bean definition:
<property name="template" value="classpath:/backupData.properties">
or
Resource template = ctx.getResource("classpath:/backupData.properties");
or the plain old as suggested by @Sotirios Delimanolis
Respondido el 28 de enero de 14 a las 18:01
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas java spring-mvc properties or haz tu propia pregunta.
do you check with absolute path ?? - Kick