Search code examples
maven-3snakeyaml

SnakeYaml class not found exception


When I parse config.yaml using SnakeYaml 1.14 I get a "Class not found exception". The following is the code used for parsing. I have used maven to build the project.

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;
        }
    }
}

Solution

  • I have found a similar error but dumping a file.

    You could write the complete name of the class in yaml.load instruction.

    For example, if AppConfiguration.class was in org.example.package1, you would write something like:

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