Saturday, 16 August 2014

Unable to load XML configuration file from relative path



My Java program is supposed to load a configuration file from a relative path. First, the configuration file was loaded from a full path and it's worked fine. Now i'm trying to load the configuration file from the relative path, so at the end the configuration file will be loaded from the same path of the exe file.


Here is the code. could anyone tell my what wrong with that? I marked the problematic line with a comment.


Main class:



public class App {
private static Object string;

public static void main(String[] args) {
CsvConfiguration conf = CsvConfiguration.getInstance();
}


CsvConfiguration class (relevant parts):



public class CsvConfiguration {

private static CsvConfiguration instance = null;
private static Document doc = null;

static{
URI path;
try {
path = Thread.currentThread().getClass().getClassLoader().getResource("/csv-generator/src/main/config/config.xml").toURI(); //my program is collapsing in this line
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(new File(path));
doc.getDocumentElement().normalize();
} catch (URISyntaxException e) {
throw new RuntimeException("Config is unparsable");
} catch (ParserConfigurationException e) {
throw new RuntimeException(e);
} catch (SAXException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}

}

private CsvConfiguration(){
try{
setGenerationRate();
setNumberOfRows();
setNumberOfColumns();
setOutputFolder();
}catch(NumberFormatException nExc){
throw new RuntimeException(nExc);
} catch (IOException e) {
throw new RuntimeException(e);
}

setColumnsList();

}

public synchronized static CsvConfiguration getInstance(){
if (instance==null)
instance = new CsvConfiguration();
return instance;
}

No comments:

Post a Comment