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.