If I use FileSystems.getDefault().getPath() it holds resource at FileSystems.getDefault(). Few tools like sonar, coverity give high impact issues of resource leak for using this.
If I replace it with Paths.get() all the such tools quietly accept this and there is no error or warning of resource leak.
If we see the implementation of Paths.get(), it is literally doing FileSystems.getDefault().getPath()
My question here is, how does java handle resource leak for Paths.get() because the code is exactly same but we don't have reference to FileSystems.getDefault() to explicitly close it?
Your tools are reporting a false positive. That's because the declared type returned by
FileSystems.getDefault(),FileSystem, implementsCloseable. That means that ideally you should close it. However, the default file system is an exception (it even throws anUnsupportedOperationException). Your tools cannot make that distinction.This happens more often than you'd think. Some examples I've seen too often:
Objects.requireNonNull. The input is returned as-is, but if the input isAutoCloseablemy IDE treats it as a new resource.There are some cases where tools and IDEs can be smart. For instance, if I declare a variable as
ByteArrayInputStream,ByteArrayOutputStream,StringReaderorStringWriter, then my IDE knows they don't need to be closed. However, when I return these from a method asInputStream,OutputStream,ReaderorWriterrespectively, my IDE starts complaining if I don't close them.If you know that it is a false positive, you can use
@SuppressWarnings("resource")to ignore warnings by some tools. That can often be applied to single variables:However, even then sometimes your tools will complain, and you have to suppress the resource warnings for the entire method. If that's the case, try to keep your method (and therefore the scope of the warning suppression) as small as possible; split off code to a new method if needed.