javayamlinputstreamsnakeyaml

Stream Closed error for accessing .yml file


Unable to access the .yml file as the it is not getting read. Not sure if the path is wrong or that given I am on Mac, I need different way for path setup.

I am trying to access .yml which is housed under src/yml/server.yml and getting the Stream Closed error.

Test class is located here for reference:

src/smokeTest/java/package.package.../SmokeTest.java

Code:

Yaml yaml = new Yaml();
InputStream ymlReader = this.getClass().getClassLoader().getResourceAsStream("yml/server.yml");

Map<String, Object> yamlMaps = yaml.load(ymlReader);

LOG.info("Value of Status Flag", yamlMaps.get("status"));

Error:

org.yaml.snakeyaml.error.YAMLException: java.io.IOException: Stream closed
    at app//org.yaml.snakeyaml.reader.StreamReader.update(StreamReader.java:218)
    at app//org.yaml.snakeyaml.reader.StreamReader.ensureEnoughData(StreamReader.java:176)
    at app//org.yaml.snakeyaml.reader.StreamReader.ensureEnoughData(StreamReader.java:171)
    at app//org.yaml.snakeyaml.reader.StreamReader.peek(StreamReader.java:126)
    at app//org.yaml.snakeyaml.scanner.ScannerImpl.scanToNextToken(ScannerImpl.java:1223)
    at app//org.yaml.snakeyaml.scanner.ScannerImpl.fetchMoreTokens(ScannerImpl.java:328)
    at app//org.yaml.snakeyaml.scanner.ScannerImpl.checkToken(ScannerImpl.java:250)
    at app//org.yaml.snakeyaml.parser.ParserImpl$ParseImplicitDocumentStart.produce(ParserImpl.java:214)
    at app//org.yaml.snakeyaml.parser.ParserImpl.peekEvent(ParserImpl.java:166)
    at app//org.yaml.snakeyaml.parser.ParserImpl.checkEvent(ParserImpl.java:156)
    at app//org.yaml.snakeyaml.composer.Composer.getSingleNode(Composer.java:145)
    at app//org.yaml.snakeyaml.constructor.BaseConstructor.getSingleData(BaseConstructor.java:157)
    at app//org.yaml.snakeyaml.Yaml.loadFromReader(Yaml.java:491)
    at app//org.yaml.snakeyaml.Yaml.load(Yaml.java:429)

I tried the solutions from these but they did not work:

Getting "java.io.IOException: Stream closed", when trying to read file from relative path

SnakeYaml keep IOException: Stream closed


Solution

  • this.getClass().getClassLoader().getResourceAsStream("yml/server.yml");
    

    This will only get the files below the current classpath. You should change the path of the yml to make sure it is in the current classloader directory