@@ -15,6 +15,7 @@ package scala.tools.nsc.classpath
15
15
import java .io .{Closeable , File }
16
16
import java .net .{URI , URL }
17
17
import java .nio .file ._
18
+ import java .util .Collections
18
19
19
20
import scala .jdk .CollectionConverters ._
20
21
import scala .reflect .internal .JDK9Reflectors
@@ -137,9 +138,9 @@ trait JFileDirectoryLookup[FileEntryType <: ClassRepresentation] extends Directo
137
138
}
138
139
139
140
object JrtClassPath {
140
- private val jrtClassPathCache = new FileBasedCache [Unit , JrtClassPath ]()
141
+ private val jrtClassPathCache = new FileBasedCache [Option [ String ] , JrtClassPath ]()
141
142
private val ctSymClassPathCache = new FileBasedCache [String , CtSymClassPath ]()
142
- def apply (release : Option [String ], unsafe : Option [List [String ]], closeableRegistry : CloseableRegistry ): List [ClassPath ] =
143
+ def apply (release : Option [String ], systemPath : Option [ String ], unsafe : Option [List [String ]], closeableRegistry : CloseableRegistry ): List [ClassPath ] =
143
144
if (! isJavaAtLeast(" 9" )) Nil
144
145
else {
145
146
// TODO escalate errors once we're sure they are fatal
@@ -155,14 +156,14 @@ object JrtClassPath {
155
156
val ct = createCt(version, closeableRegistry)
156
157
unsafe match {
157
158
case Some (pkgs) if pkgs.nonEmpty =>
158
- createJrt(closeableRegistry) match {
159
+ createJrt(systemPath, closeableRegistry) match {
159
160
case Nil => ct
160
161
case jrts => ct.appended(new FilteringJrtClassPath (jrts.head, pkgs : _* ))
161
162
}
162
163
case _ => ct
163
164
}
164
165
case _ =>
165
- createJrt(closeableRegistry)
166
+ createJrt(systemPath, closeableRegistry)
166
167
}
167
168
}
168
169
private def createCt (v : String , closeableRegistry : CloseableRegistry ): List [ClassPath ] =
@@ -176,10 +177,15 @@ object JrtClassPath {
176
177
} catch {
177
178
case NonFatal (_) => Nil
178
179
}
179
- private def createJrt (closeableRegistry : CloseableRegistry ): List [JrtClassPath ] =
180
+ private def createJrt (systemPath : Option [ String ], closeableRegistry : CloseableRegistry ): List [JrtClassPath ] =
180
181
try {
181
- val fs = FileSystems .getFileSystem(URI .create(" jrt:/" ))
182
- val classPath = jrtClassPathCache.getOrCreate((), Nil , () => new JrtClassPath (fs), closeableRegistry, checkStamps = false )
182
+ val classPath = jrtClassPathCache.getOrCreate(systemPath, Nil , () => {
183
+ val fs = systemPath match {
184
+ case Some (javaHome) => FileSystems .newFileSystem(URI .create(" jrt:/" ), Collections .singletonMap(" java.home" , javaHome))
185
+ case None => FileSystems .getFileSystem(URI .create(" jrt:/" ))
186
+ }
187
+ new JrtClassPath (fs, systemPath.isDefined)
188
+ }, closeableRegistry, checkStamps = false )
183
189
List (classPath)
184
190
} catch {
185
191
case _ : ProviderNotFoundException | _ : FileSystemNotFoundException => Nil
@@ -207,7 +213,7 @@ final class FilteringJrtClassPath(delegate: JrtClassPath, allowed: String*) exte
207
213
*
208
214
* The implementation assumes that no classes exist in the empty package.
209
215
*/
210
- final class JrtClassPath (fs : FileSystem ) extends ClassPath with NoSourcePaths {
216
+ final class JrtClassPath (fs : FileSystem , closeFS : Boolean ) extends ClassPath with NoSourcePaths with Closeable {
211
217
type F = Path
212
218
private val dir : Path = fs.getPath(" /packages" )
213
219
@@ -253,6 +259,9 @@ final class JrtClassPath(fs: FileSystem) extends ClassPath with NoSourcePaths {
253
259
}.take(1 ).toList.headOption
254
260
}
255
261
}
262
+
263
+ def close (): Unit =
264
+ if (closeFS) fs.close()
256
265
}
257
266
258
267
/**
0 commit comments