mardi 3 mars 2015

How to add a file.properties to classpath in NetBeans?



I'm not able to add a custom file.properties to my classpath in NetBeans. This is my root directory: http://ift.tt/1F6KKnR


I created a resources directory where puts all my properties file,


/src/java <-- root classpath folder /src/resources <-- properties file (like dao.properties)


If I put the dao.properties in the root classpath, i'm able to load it: /src/java/dao.properties


If I put the dao.properties in any other subpkg, it will not be found (classpath error)



  • /src/java/dao/dao.properties

  • /src/java/main/dao.properties

  • /src/java/utils/dao.properties


and so on...


The loading of properties is made by using the class provided by Bauke Scholtz:



/*
* 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 dao;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
* This class immediately loads the DAO properties file 'dao.properties'
* once in memory and provides a constructor which takes the specific key
* which is to be used as property key prefix of the DAO properties file.
* There is a property getter which only returns the property prefixed
* with 'specificKey.' and provides the option to indicate whether the
* property is mandatory or not.
*
* @author BalusC
* @link http://ift.tt/NWaSLe
*/
public class DAOProperties {

// Constants ------------------------------------------------------
private static final String PROPERTIES_FILE = "dao.properties";
private static final Properties PROPERTIES = new Properties();

static {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream propertiesFile = classLoader.getResourceAsStream(PROPERTIES_FILE);

if (propertiesFile == null) {
throw new DAOConfigurationException(
"Properties file '" + PROPERTIES_FILE + "' is missing in classpath.");
}

try {
PROPERTIES.load(propertiesFile);
} catch (IOException e) {
throw new DAOConfigurationException(
"Cannot load properties file '" + PROPERTIES_FILE + "'.", e);
}
}

// Vars -----------------------------------------------------------
private String specificKey;

// Constructors ---------------------------------------------------
/**
* Construct a DAOProperties instance for the given specific key which
* is to be used as property key prefix of the DAO properties file.
*
* @param specificKey The specific key which is to be used as property
* key prefix.
* @throws DAOConfigurationException During class initialization if the
* DAO properties file is missing in the classpath or cannot be loaded.
*/
public DAOProperties(String specificKey) throws DAOConfigurationException {
this.specificKey = specificKey;
}

// Actions --------------------------------------------------------
/**
* Returns the DAOProperties instance specific property value associated
* with the given key with the option to indicate whether the property is
* mandatory or not.
*
* @param key The key to be associated with a DAOProperties instance
* specific value.
* @param mandatory Sets whether the returned property value should not be
* null nor empty.
* @return The DAOProperties instance specific property value associated
* with the given key.
* @throws DAOConfigurationException If the returned property value is null
* or empty while it is mandatory.
*/
public String getProperty(String key, boolean mandatory)
throws DAOConfigurationException {
String fullKey = specificKey + "." + key;
String property = PROPERTIES.getProperty(fullKey);

if (property == null || property.trim().length() == 0) {
if (mandatory) {
throw new DAOConfigurationException(
"Required property '" + fullKey + "'" +
" is missing in properties file '" +
PROPERTIES_FILE + "'.");
} else {
// Make empty value null. Empty Strings are evil.
property = null;
}
}

return property;
}
}


Thanks in advance for the support, is the whole day i'm fighting with google and netbeans to find the answer.




Aucun commentaire:

Enregistrer un commentaire