# \[SOLVED\] Try with resources with \`AutoClosable\`

**URL:** <https://discuss.kotlinlang.org/t/solved-try-with-resources-with-autoclosable/2606>\
**Category:** Support\
**Created:** [May 15, 2017, 2:28am UTC](https://discuss.kotlinlang.org/t/solved-try-with-resources-with-autoclosable/2606 "2017-05-15T02:28:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![trajano](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/trajano/32/2519_2.png) [@trajano](https://discuss.kotlinlang.org/u/trajano)\
**Post date:** [May 15, 2017, 2:28am UTC](https://discuss.kotlinlang.org/t/solved-try-with-resources-with-autoclosable/2606/1 "2017-05-15T02:28:15Z")

</div>

I’m using TensorFlow which provides

```
public final class Graph implements AutoCloseable

```

However I am getting an error when I try to use the try-with-resources construct of Kotlin like this…

```
Graph().use {
    ....
}

```

When I look into it, it appears that `use` requires a `Closeable` and does not work with an `AutoClosable` and `AutoClosable` does not inherit from `Closable` and is only available in JDK7 and up.

Is there another way around this?

I just noticed this from [Kotlin needs try-with-resources](https://discuss.kotlinlang.org/t/kotlin-needs-try-with-resources/214)

> Here are the problems with the Closeable.use extension method:
> 
> - Doesn’t work with Autocloseable (easy to fix, but seriously? this is a can of worms, but Kotlin’s stdlib needs to be 100% compatible with whatever the latest JDK is. In fact, you probably need to version the stdlib (and, perhaps, Kotlin itself) together with the JDK.)

---

<div class="post-metadata">

**Author:** ![yole](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/yole/32/1425_2.png) [@yole](https://discuss.kotlinlang.org/u/yole)\
**Post date:** [May 15, 2017, 6:55am UTC](https://discuss.kotlinlang.org/t/solved-try-with-resources-with-autoclosable/2606/2 "2017-05-15T06:55:33Z")

</div>

You need to add `kotlin-stdlib-jre7` or `kotlin-stdlib-jre8` as a dependency to your project; it defines a version of `use` that works with `AutoCloseable`.

---

<div class="post-metadata">

**Author:** ![trajano](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/trajano/32/2519_2.png) [@trajano](https://discuss.kotlinlang.org/u/trajano)\
**Post date:** [May 15, 2017, 3:30pm UTC](https://discuss.kotlinlang.org/t/solved-try-with-resources-with-autoclosable/2606/3 "2017-05-15T15:30:02Z")

</div>

Thanks that worked!

---

<div class="post-metadata">

**Author:** ![jaccozilla](https://avatars.discourse-cdn.com/v4/letter/j/58f4c7/32.png) [@jaccozilla](https://discuss.kotlinlang.org/u/jaccozilla)\
**Post date:** [December 7, 2017, 11:21am UTC](https://discuss.kotlinlang.org/t/solved-try-with-resources-with-autoclosable/2606/4 "2017-12-07T11:21:55Z")

</div>

I’m running into this same problem in kotlin 1.2 using kotlin-stdlib-jdk8. If I change back to 1.1.50 and kotlin-stdlib-jre8 it compiles fine.
