2014-10-20 99 views
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; 
     } 
    } 
} 

回答

0

我發現了類似的錯誤,但是dumping a file

您可以在yaml.load指令中編寫類的完整名稱。

例如,如果AppConfiguration.classorg.example.package1,你會寫是這樣的:

yaml.loadAs(fileContents, org.example.package1.AppConfiguration.class);