Error reading java properties file

Asked

Viewed 1,233 times

1

I do not know if it is correct and if it works, but I realized a division in the project. a project would access the DAO and have the connection and a properties file with the connection information. Another has a main class in which I haven’t created frames yet, just a main class for testing. In libraries I added the projects as reference and I did the test but is giving an error where can not read the properties file, could tell me what can be?

Description of the Error:

jul 27, 2016 9:30:41 AM Conection.GenericConnection getDbProperties
GRAVE: null
java.io.FileNotFoundException: src\properties\conf.properties (O sistema não pode encontrar o caminho especificado)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(FileInputStream.java:195)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at java.io.FileInputStream.<init>(FileInputStream.java:93)
    at Conection.GenericConnection.getDbProperties(GenericConnection.java:25)
    at Conection.OracleConnection.getConnection(OracleConnection.java:35)
    at Views.mainTeste.main(mainTeste.java:16)

Exception in thread "main" java.lang.NullPointerException
    at Conection.OracleConnection.getConnection(OracleConnection.java:35)
    at Views.mainTeste.main(mainTeste.java:16)
D:\SICF_Project\SICFCadastros\nbproject\build-impl.xml:1063: The following error occurred while executing this line:
D:\SICF_Project\SICFCadastros\nbproject\build-impl.xml:804: Java returned: 1
FALHA NA CONSTRUÇÃO (tempo total: 0 segundos)

Class Mother Genericconnection

package Conection;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.Statement;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Ulisses Gimenes
 */
public abstract class GenericConnection {

    protected Properties dbProperties;
    protected Connection conn;
    protected Statement st;

    public Properties getDbProperties() {
        if (dbProperties == null) {
            try {
                dbProperties.load(new FileInputStream("src/properties/conf.properties"));
            } catch (FileNotFoundException ex) {
                Logger.getLogger(GenericConnection.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(GenericConnection.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return dbProperties;
    }

    public void setDbProperties(Properties dbProperties) {
        this.dbProperties = dbProperties;
    }

    public Connection getConn() {
        return conn;
    }

    public void setConn(Connection conn) {
        this.conn = conn;
    }

    public Statement getSt() {
        return st;
    }

    public void setSt(Statement st) {
        this.st = st;
    }

    public abstract Connection getConnection();
}

Connection class for oracle

package Conection;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author UlissesGimenes
 */
public class OracleConnection extends GenericConnection {  

    private static OracleConnection connOra;

    public static OracleConnection getConnOra() {
        if(connOra == null){
            connOra = new OracleConnection();
        }
        return connOra;
    }

    public static void setConnOra(OracleConnection connOra) {
        OracleConnection.connOra = connOra;
    }    

    @Override
    public Connection getConnection() {
        if (getConn() != null) {
            return getConn();
        } else {
            try {
                String url = "jdbc:oracle:thin:@" + 
                        super.getDbProperties().getProperty("ServerOracle") + 
                        ":" + super.getDbProperties().getProperty("portOracle") + 
                        ":" + super.getDbProperties().getProperty("sidOracle");
                setConn(DriverManager.getConnection(
                        url, 
                        super.getDbProperties().getProperty("userOracle"), 
                        super.getDbProperties().getProperty("passwdOracle")));
                setSt(getConn().createStatement());
                System.out.println("conectado");
                return getConn();
            } catch (SQLException ex) {
                Logger.getLogger(OracleConnection.class.getName()).log(Level.SEVERE, null, ex);
                return null;
            }
        }
    }

}

Main class of testing

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Views;

import Conection.OracleConnection;

/**
 *
 * @author UlissesGimenes
 */
public class mainTeste {
    public static void main(String[] args) {
        OracleConnection.getConnOra().getConnection();
    }
}

Image of the tree of the project

inserir a descrição da imagem aqui

  • When formatting code, use the shortcut {} to format the code, since the snippet is for html, javascript or css. Add the image directly here, the link may not be accessible for some people.

  • Yes, Thank you...

  • I had forgotten, of a new Properties();, as I also could not recognize the properties folder, I put it inside the project that will be executed.

  • diegofm, In the comments, I can put the link to an image..

  • Recommended is to insert directly into the question.

  • thanks, thanks...

Show 1 more comment

2 answers

1


Your problem is in detecting the path correct file . properties in the project. The point is that the way you are accessing the file, it should be in the working directory, which can be accessed this way:

System.getProperty("user.dir")

Now, if you don’t want to leave the file. properties in the working directory and prefer that it goes packaged in the project, then you should access it via getClass(). getResource() or getClass(). getResourceAsStream().

  • I don’t know if it was correct, but I am trying to implement a test application in modules, I put the properties inside the project that will be application, packaged as you said, not aware of this command System.getProperty("user.dir"), it searches inside the user directory, bad if you have more than one user?

  • Have a look at the tree. https://drive.google.com/file/0B91j9PUXTgJscmpqT3JGWnZ3VDQ/view?usp=sharing

  • I ran the main class of the Application project, and connected in the database by the Dataacessobj project, what is your opinion.

  • Odysseus, look at, System.getProperty("user.dir") will not return you the user directory, but, yes, the working directory, ie the directory in which the application is running.

  • Thank you, thank you.

1

Apparently you are indicated the wrong way to the file. I don’t know if I got your project tree right because I don’t work with this IDE but try changing this path:

src/properties/conf.properties

By this path:

properties/conf.properties

It is always better to keep this type of configuration file in the root path of the project, so it is easier to find. Just take the main project and create a folder inside it properties and put the file inside.

  • I noticed, he did not recognize the property file, when I get home I will give a analyzed, on the possibility of putting in the root of the project.. I have a lot to learn... Thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.