我有一个名为的类JavaShellStream
,它扩展了接口Closeable
和Autocloseable
. 但是,当我使用该类并且不调用其close()
方法时,不会触发任何警告,表明存在资源泄漏。我有另一个类JavaShell
,它有一个exec(String cmd)
返回JavaShellStream
对象的方法。JavaShellStream
当我通过以下方式创建新对象时,不会触发资源泄漏警告:
//workingDir is a File object that sets the working directory
JavaShell js = new JavaShell(workingDir);
//execute the "ls" command and retrieve output as a JavaShellStream
JavaShellStream jss = js.exec("ls");
/*
* some code to read the text from the stream
* but DOES NOT close the stream
*/
// Note that I am NOT calling the close() method
为什么这里没有触发警告说存在资源泄漏,因为JavaShellStream jss
从未关闭?这里的答案说触发它所需要的只是实现 interface Closeable
,但是我的类实现了这两者Closeable
并且Autocloseable
在未关闭时不会触发任何警告。