We have a maven project with some files in the resources dir which get copied into the root of the jar file. I have the following bit of code which works fine during JUnit testing but stops working once I try to execute it from the jar
Configuration configuration = new Configuration();
String pathString = MainClass.class.getClassLoader().getResource("dir").getPath();
Path path = new Path(pathString);
logger.debug(path);
FileSystem fs = path.getFileSystem(configuration);
if (fs.exists(path)) {
logger.debug("WOOOOO");
} else {
logger.debug("BOOOOO");
}
While testing, the output is:
DEBUG: /path/to/project/target/test-classes/dir
DEBUG: WOOOOO
While running from jar I get:
DEBUG file:/path/to/jar/project.jar!/dir
DEBUG BOOOOO
Needless to say, the jar file is in the correct location and the dir is in the root of that jar.
In case you're wondering why we're doing this, the second half is little test excerpt, which mimics what NaiveBayesModel.materialize() in Mahout does. We just need to be able to create a path that Mahout will understand.