0

I'm trying to write a simple code to communicate with the database. But it gives an error. The application.properties file contains a link to the localhost, username and password. In three lines. image exception text

Main.java

    public static void main(String[] args) throws SQLException {
        Class<Driver> driverClass = Driver.class;
        try (var connection = ConnectionManager.open()) {
            System.out.println(connection.getTransactionIsolation());
        }
    }
}

ConnectionManager.java

public final class ConnectionManager {

    private static final String PASSWORD_KEY = "db.password";
    private static final String USERNAME_KEY = "db.username";
    private static final String URL_KEY = "db.url";

    static {
        loadDriver();
    }
    private ConnectionManager() {
    }
    public static Connection open() {
        try {
            return DriverManager.getConnection(
                    PropertiesUtil.get(URL_KEY),
                    PropertiesUtil.get(USERNAME_KEY),
                    PropertiesUtil.get(PASSWORD_KEY)
            );
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    private static void loadDriver() {
        try {
            Class.forName("org.postgresql.Driver");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}

PropertiesUtil.java

public final class PropertiesUtil {
    private static final Properties PROPERTIES = new Properties();
    static {
        loadProperties();
    }
    private PropertiesUtil() {
    }
    public static String get(String key) {
        return PROPERTIES.getProperty(key);
    }
    private static void loadProperties() {
        try (var inputStream = PropertiesUtil.class.getClassLoader().getResourceAsStream("application.properties")) {
            PROPERTIES.load(inputStream);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

project

pgAdmin4

2 Answers 2

1

The problem was solved by moving the application.properties file to the root directory in src. In idea, you can mark the file as "resources root" and the file seems to be in the root directory. I didn't find this in vs code.

Sign up to request clarification or add additional context in comments.

Comments

0

Learn how to understand errors. The error is clearly saying that inputStream in the line PROPERTIES.load(inputStream) is null. This is because:

PropertiesUtil.class.getClassLoader().getResourceAsStream("application.properties")

is trying to load a resource that does not exist. I think you're misunderstanding / mis-using gRAS here:

  • The proper syntax is PropertiesUtil.class.getResourceAsStream("/application.properties")
  • Regardless, it's a system designed for static, as in unchanging resources. A property file is presumably intended to be editable by the user. Hence it has no business being loaded like this - it's for resources you e.g. pack into the jar file, which you wouldn't do to a properties file.

There is no non-hacky/easy way to find 'the location where my jar resides', so you may not want to do that. One easy solution is to load from the user's home dir:

try (var in = Files.newInputStream(Paths.get(System.getProperty("user.home"), "application.properties"))) {
   PROPERTIES.load(in);
}

3 Comments

When you add application.properties to user.home according to your advice, everything works. But, I still can't understand why my code doesn't work if application.properties is in the project folder at resources\\application.properties? By the way, if you write this way PropertiesUtil.class.getResourceAsStream("/application.properties") it doesn't work anyway. The question is also that I do it from online lessons. Everything works for the one who teaches the lesson. And for some reason I get an exception.
The info you provided isn't sufficient to tell you anything meaningful. Either [A] resources isn't on the classpath (when running in IDE/straight off of the built class files), or [B] whatever tool created the jar for you (maven, presumably) did not include the file in the jar.
And whomever is teaching this doesn't understand this part of java. Nobody is an expert at every nook and cranny, but not a good look.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.