1
當我使用SnakeYaml 1.14解析config.yaml時,我得到一個「Class not found exception」。以下是用於解析的代碼。我已經使用maven來構建這個項目。SnakeYaml類沒有發現異常
public class AppConfigurationReader
{
private static final String CONFIG_FILE = "config.yaml";
private static String fileContents = null;
private static final Logger logger = LoggerFactory.getLogger(AppConfigurationReader.class);
public static synchronized AppConfiguration getConfiguration() {
return getConfiguration(false);
}
public static synchronized AppConfiguration getConfiguration(Boolean forceReload) {
try {
Yaml yaml = new Yaml();
if(null == fileContents || forceReload) {
fileContents = read(CONFIG_FILE);
}
yaml.loadAs(fileContents, AppConfiguration.class);
return yaml.loadAs(fileContents, AppConfiguration.class);
}
catch (Exception ex) {
ex.printStackTrace();
logger.error("Error loading fileContents {}", ex.getStackTrace()[0]);
return null;
}
}
private static String read(String filename) {
try {
return new Scanner(new File(filename)).useDelimiter("\\A").next();
} catch (Exception ex) {
logger.error("Error scanning configuration file {}", filename);
return null;
}
}
}