2

I have a class called JavaShellStream that extends the interfaces Closeable and Autocloseable. However, when I use the class, and do not call its close() method, no warning is triggered that there is a resource leak. I have another class called JavaShell which has a method called exec(String cmd) that returns a JavaShellStream object. When I create a new JavaShellStream object in the following way, no resource leak warning is triggered:

//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

Why is no warning triggered here that there is a resource leak because the JavaShellStream jss is never closed? The answer from here says that all that's required to trigger it is to implement the interface Closeable, but my class implements both Closeable and Autocloseable but will not trigger any warning when unclosed.

Community
  • 1
  • 1
Mat Jones
  • 936
  • 1
  • 10
  • 27
  • I have the same issue. The issue is caused when a resource instance is returned by a method instead of newly created. Unfortunately there is no solution for that (yet). – ST-DDT Apr 11 '17 at 07:32

1 Answers1

0

If you are using Eclipse (as the linked question relates to), there is an option to display a warning where there is a possibility of a resource leak.

Go to Window > Preferences > Java > Compiler > Errors/Warnings, and set the severity level of "Potential resource leak" to Warning.

M A
  • 71,713
  • 13
  • 134
  • 174