title: Stream is closed when accessing resources file from a library jar file
body: <p>I have a project that has this structure and I call this as LIBRARY jar file..I packaged it as a jar file..</p>
  
  <pre><code>/src/main/java
      /src/main/resources/files.yaml
  
  public class DataManager {
  
      public static String FILE = "files.yaml";
  
      public Optional&lt;Object&gt; getFiles(String code) throws YAMLException {
          Yaml yaml = new Yaml();
  
          ClassLoader classLoader = getClass().getClassLoader();
          File featureFile = new File(getFile());
          if (featureFile.exists()) {
              logger.info("Feature file is present!!!");
          } else {
              // This is called
              logger.info("Feature file NOT PRESENT!!!");
          }
  
          try (InputStream in = classLoader.getResourceAsStream(getFile())) {
              //Stream is closed at this point
              yaml.load(in);
          } catch (IOException e) {
              throw new YAMLException("Unable to read  file", e);
          }
  
          return Optional.empty();
  
      }
  
      protected String getFile() {
          return FILE;
      }
  
  }
  </code></pre>
  
  <p>Now, in my MAIN project..I am referencing that LIBRARY jar file</p>
  
  <pre><code>public class Main {
      public void getSomething(String code) throws YAMLException {
  
          DataManager manager = new DataManager();
          manager.getFiles("SAMPLE");
          Yaml yaml = new Yaml();
      }
  }
  </code></pre>
  
  <p>When I call my DataManager from my main class, I am getting stream is closed and I think it cannot find the file.
  I have used classloader to fetch the file for me but it seems that I might be doing something.</p>
  
  <p>I did a <code>jar tvf library.jar</code> and I saw that my config file "files.yaml" are packaged in my library jar file.  What could be wrong?</p>
  
  <p><strong>UPDATE 2</strong>
  What is weird is that, I tried to read the file using buffered reader.</p>
  
  <pre><code>InputStream ins = this.getClass().getClassLoader().getResourceAsStream(getFile());
  try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(ins, StandardCharsets.UTF_8))) {
      while ((line = bufferedReader.readLine()) != null) {
          stringBuilder.append(line);
      }
  } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
  }
  
  // It prints the values of the yml files
  </code></pre>
  
  <p>And it was able to read the file.. so I am not sure why using my first code is not working but using bufferedreader works.  Any idea?</p>