Skip to content

Commit ef2cbc6

Browse files
committed
Closing JarFile instances
1 parent 86d8975 commit ef2cbc6

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

server/src/main/scala/org/scalaexercises/evaluator/evaluation.scala

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import scala.reflect.internal.util.ScalaClassLoader.URLClassLoader
2727
import scala.tools.nsc.io.{AbstractFile, VirtualDirectory}
2828
import scala.tools.nsc.reporters._
2929
import scala.tools.nsc.{Global, Settings}
30-
import scala.util.Try
30+
import scala.util.{Failure, Success, Try}
3131
import scala.util.control.NonFatal
3232

3333
class Evaluator[F[_]: Sync](timeout: FiniteDuration = 20.seconds)(
@@ -463,14 +463,21 @@ class ${className} extends (() => Any) with java.io.Serializable {
463463
val currentClassPath = classPath.head
464464

465465
// if there's just one thing in the classpath, and it's a jar, assume an executable jar.
466-
currentClassPath ::: (if (currentClassPath.size == 1 && currentClassPath(0)
466+
currentClassPath ::: (if (currentClassPath.size == 1 && currentClassPath.head
467467
.endsWith(".jar")) {
468-
val jarFile = currentClassPath(0)
468+
val jarFile = currentClassPath.head
469469
val relativeRoot =
470-
new File(jarFile).getParentFile()
471-
val nestedClassPath =
472-
new JarFile(jarFile).getManifest.getMainAttributes
473-
.getValue("Class-Path")
470+
new File(jarFile).getParentFile
471+
val nestedClassPath = Try {
472+
val jar = new JarFile(jarFile)
473+
val CP = jar.getManifest.getMainAttributes.getValue("Class-Path")
474+
jar.close()
475+
CP
476+
} match {
477+
case Success(classPath) => classPath
478+
case Failure(throwable) =>
479+
throw new CompilerException(List(List(throwable.getMessage)))
480+
}
474481
if (nestedClassPath eq null) {
475482
Nil
476483
} else {

0 commit comments

Comments
 (0)