Chce wczytać plik .properties tak, żeby wczytywanie działało później odpalając apkę za pomocą pliku .jar i przeczytałem na necie, że należy używać metody getResourceAsStream tak też próbuję zrobić ale dostaje wyjątek:
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: 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.wojo.Vault.Database.DBManager.getConnection(DBManager.java:157)
at com.wojo.Vault.Database.DBManager.dbConnection(DBManager.java:142)
at com.wojo.Vault.Main.connectionToDatabase(Main.java:39)
at com.wojo.Vault.Main.main(Main.java:32)
... 11 more
Exception running application com.wojo.Vault.Main
Domyślam się, że problem leży w ścieżce do pliku tylko nie mam pojęcia jak powinna wyglądać próbowałem już wielu rozwiązań :x
Oto mój kod wczytujący:
private static Connection getConnection() throws SQLException, IOException {
Properties properties = new Properties();
DBManager dbManager = new DBManager();
try (InputStream in = dbManager.getClass().getClassLoader().getResourceAsStream("/resources/META-INF/database.properties")) {
properties.load(in);
}
String drivers = properties.getProperty("jdbc.drivers");
if (drivers != null) {
System.setProperty("jdbc.drivers", drivers);
}
String url = properties.getProperty("jdbc.url");
String username = properties.getProperty("jdbc.username");
String password = properties.getProperty("jdbc.password");
return DriverManager.getConnection(url, username, password);
}
A struktura katalogów wygląda tak: