Search code examples
javayamlsnakeyaml

Copyright symbol in YAML file is rendered incorrectly in built JAR file


My Java program uses Snake YAML to parse a YAML file that contains text to be displayed to the user. Some of those strings contain the copyright symbol (©). When I run the program within my IDE (IntelliJ IDEA) the copyright symbol is rendered correctly. But once I build an artifact and run the resulting JAR file the copyright symbol is instead rendered "©" (without the quotes).

How can I either alter my program to read the file correctly or alter the YAML file so that the copyright symbol is rendered correctly?

Here is the Java code that loads the YAML.

private void loadOptions () 
        throws IOException, SAXException, ParserConfigurationException
{
  Yaml yaml = new Yaml();
  String filePath = "./config.yml";
  Reader reader = null;

try {
    reader = new FileReader(filePath);
    options = (Map<String, Map>) yaml.load(reader);
  }
  catch (FileNotFoundException e) {
    String msg = "Either the YAML file could not be found or could not be read: " + e;
    System.err.println(msg);
  }

  if (reader != null) {
    reader.close();
  }
}

Here is a sample of the YAML code in question:

text:
  copyright:
    © 2007 Acme Publishing (info@example.org)

Solution

  • Thanks to @Amadan's comment on my question, I was lead to change my Java code to the following, which fixed the problem:

    private void loadOptions ()
        throws IOException, SAXException, ParserConfigurationException
    {
      Yaml yaml = new Yaml();
      String filePath = "./config.yml";
      Reader reader = null;
    
      try {
        FileInputStream file = new FileInputStream(filePath);
        reader = new InputStreamReader(file, "UTF-8");
        options = (Map<String, Map>) yaml.load(reader);
      }
      catch (FileNotFoundException e) {
        String msg = "Either the YAML file could not be found or could not be read: " + e;
        System.err.println(msg);
      }
    
      if (reader != null) {
        reader.close();
      }
    }