0

I'm working in a Java Swing application and I'm facing a strange error when running the app outside my IDE (i.e. in pre-production). BTW, I'm using Netbeans 8.2.

The application connects to a database and takes the configuration properties from a connection.properties file located in src/main/resources, which, of course, are packaged in the root directory inside the jar.

I am able to compile the application and run it from Netbeans with no problem: The properties are loaded, the connection to the DB is done and the data is being read and updated according to the business logic and so on.

The problem comes when I try to run my app from the console. From there I run

java -jar myapp-1.0.jar

And get the following error

ago 01, 2017 8:41:25 PM ar.edu.unt.jdbc.GestorJdbc loadPropertiesFromFile
SEVERE: File not found
java.io.FileNotFoundException: file:/path/to/my/app/target/myapp-1.0.jar!/connection.properties (No such file or directory)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at ar.edu.unt.jdbc.JdbcManager.loadPropertiesFromFile(JdbcManager.java:216)
... etc

However, the file actually exists. The code from the method is as follows:

public class JdbcManager {

    static final String JDBC_DRIVER_PROPERTY = "jdbc.driver";
    static final String JDBC_URL_PROPERTY = "jdbc.url";
    static final String JDBC_USER_PROPERTY = "jdbc.username";
    static final String JDBC_PASSWORD_PROPERTY = "jdbc.password";
    String PROPERTIES_FILE_PATH = "connection.properties";

    private static final Logger LOGGER = Logger.getLogger(JdbcManager.class.getName());

    // This method is being called with PROPERTIES_FILE_PATH as a parameter
    // from another method in the same class
    Properties loadPropertiesFromFile(String configFilePath) {
        ClassLoader classLoader = getClass().getClassLoader();
        File configFile = new File(classLoader.getResource(configFilePath).getFile());
        Properties properties = new Properties();
        try {
            properties.load(new FileInputStream(configFile));
            LOGGER.log(Level.INFO, "Properties loaded: {0}", properties);
        } catch (FileNotFoundException ex) {
            LOGGER.log(Level.SEVERE, "File not found", ex);
        } catch (IOException ex) {
            LOGGER.log(Level.SEVERE, "Error reading file", ex);
        }

        return properties;
    }
}

As I said, from Netbeans the file is read correctly. What am I doing wrong here? Thanks in advance for your answers.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Alvaro Pedraza
  • 1,176
  • 6
  • 20
  • 44
  • 1
    Application resources will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. An [tag:embedded-resource] must be accessed by URL rather than file. See the [info. page for embedded resource](http://stackoverflow.com/tags/embedded-resource/info) for how to form the URL. – Andrew Thompson Aug 02 '17 at 21:48
  • 1
    @AndrewThompson you're right, now the problem is solved (I've been discovering others in the same app) with the solution in the question you've quoted. Thank you so much for your time – Alvaro Pedraza Aug 04 '17 at 01:23
  • Glad you got it (or at least are getting it) sorted. :) – Andrew Thompson Aug 04 '17 at 02:38

0 Answers0