Dies ist mein Code zum Lesen der Datei .properties in meinem Projekt.Eigenschaftendatei nicht im Jar gefunden
public class Configuration {
private static JSONObject readConfigFile(File filename) throws JSONException {
JSONObject configProperties = new JSONObject();
Properties prop = new Properties();
InputStream input = null;
try {
input = new FileInputStream(filename);
// load a properties file
prop.load(input);
// get the property value and print it out
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return configProperties;
}
public static JSONObject loadConfigurationDetails(String configFilePath) throws JSONException {
JSONObject configurationDetails = new JSONObject();
File configFile = new File(configFilePath);
if(configFile.exists()){
configurationDetails = readConfigFile(configFile);
}else{
System.out.println("configuration.properties file not found");
System.exit(0);
}
return configurationDetails;
}
public static JSONObject loadConfigurationDetails() throws JSONException {
String configFilePath = "configuration.properties";
JSONObject loadConfigurationDetails = loadConfigurationDetails(configFilePath);
return loadConfigurationDetails;
}
}
Das folgende ist meine Projektstruktur
Projektname
-src
-target
-properties
-pom.xml
Sie meine Projektstruktur anzeigen kann Datei: link
Meine Eigenschaftsdatei ist un der Wurzelprojekt. Ich kann diese Datei nicht lesen, wenn ich eine JAR-Datei erstelle und sie ausführe. Ich bekomme Datei nicht für die Eigenschaftendatei gefunden.
In meinem Haupt Methode, die ich meine Methode nenne wie folgt:
JSONObject loadConfigurationDetails = Configuration.loadConfigurationDetails();
Modified-Code und Fehler
public class Configuration {
public static JSONObject loadConfigurationDetails() throws JSONException {
Properties prop = new Properties();
String filePath = "";
JSONObject configProperties = new JSONObject();
try {
InputStream inputStream =
Configuration.class.getClassLoader().getResourceAsStream("configuration.properties");
prop.load(inputStream);
configProperties.put("URL",prop.getProperty("URL",""));
} catch (IOException e) {
e.printStackTrace();
}
return configProperties;
}
}
Exception in thread "main" java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Properties.java:434)
at java.util.Properties.load0(Properties.java:353)
at java.util.Properties.load(Properties.java:341)
at com.demo.main.Configuration.loadConfigurationDetails(Configuration.java:33)
at com.demo.main.Run.main(Run.java:30)
Ressourcen sind keine Dateien und kann nicht mit 'FileInputStream' oder seinen Verwandten erreicht werden. Sie sollten 'Class.getResource()' und Freunde verwenden. – EJP
Bitte schauen Sie sich meinen modifizierten Code und meine Projektstruktur an. In meinem modifizierten Code bekomme ich Nullzeiger Ausnahme, weil die Datei nicht gefunden wird. Bitte sehen Sie meine Projektstruktur. Danke für Ihre Antwort. –
Ich habe den Code von Alan Hay Sir in den Link von MKYONG –