Hiii ... Chcę pobrać zawartość pliku właściwości do obiektu klasy InputStream za pomocą metody getSystemResourceAsStream(). Zbudowałem przykładowy kod. Działa to dobrze przy użyciu metody main(), ale gdy wdrażam projekt i uruchamiam go na serwerze, ścieżka pliku właściwości nie może zostać uzyskana ... tak więc wartość obiektu magazynu typu inputstream ma wartość null.getSystemResourceAsStream() zwraca wartość null
Przykładowy kod jest tutaj ..
public class ReadPropertyFromFile {
public static Logger logger = Logger.getLogger(ReadPropertyFromFile.class);
public static String readProperty(String fileName, String propertyName) {
String value = null;
try {
//fileName = "api.properties";
//propertyName = "api_loginid";
System.out.println("11111111...In the read proprty file.....");
// ClassLoader loader = ClassLoader.getSystemClassLoader();
InputStream inStream = ClassLoader.getSystemResourceAsStream(fileName);
System.out.println("In the read proprty file.....");
System.out.println("File Name :" + fileName);
System.out.println("instream = "+inStream);
Properties prop = new Properties();
try {
prop.load(inStream);
value = prop.getProperty(propertyName);
} catch (Exception e) {
logger.warn("Error occured while reading property " + propertyName + " = ", e);
return null;
}
} catch (Exception e) {
System.out.println("Exception = " + e);
}
return value;
}
public static void main(String args[]) {
System.out.println("prop value = " + ReadPropertyFromFile.readProperty("api.properties", "api_loginid"));
}
}
Dlaczego używasz programu ładującego klasy, a nie normalnego? – skaffman