Skip to content

Commit a3817c8

Browse files
committed
Try to find tools.jar automatically
1 parent a175904 commit a3817c8

File tree

4 files changed

+14
-10
lines changed

4 files changed

+14
-10
lines changed

src/main/kotlin/com/tschuchort/compiletesting/JavacUtils.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,5 +100,9 @@ internal fun isJavac9OrLater(javacVersionString: String): Boolean {
100100
}
101101

102102
/** Finds the tools.jar given a path to a JDK 8 or earlier */
103-
internal fun findToolsJarFromJdk(jdkHome: File): File
104-
= File(jdkHome.absolutePath + "/../lib/tools.jar").also { check(it.isFile) }
103+
internal fun findToolsJarFromJdk(jdkHome: File): File {
104+
return jdkHome.resolve("../lib/tools.jar").existsOrNull()
105+
?: jdkHome.resolve("lib/tools.jar").existsOrNull()
106+
?: jdkHome.resolve("tools.jar").existsOrNull()
107+
?: throw IllegalStateException("Can not find tools.jar from JDK with path ${jdkHome.absolutePath}")
108+
}

src/main/kotlin/com/tschuchort/compiletesting/KotlinCompilation.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,11 @@ class KotlinCompilation {
270270
* internal compiler error!
271271
*/
272272
var toolsJar: File? by default {
273-
findInHostClasspath(hostClasspaths, "tools.jar", Regex("tools.jar"))
273+
if (!isJdk9OrLater())
274+
jdkHome?.let { findToolsJarFromJdk(it) }
275+
?: findInHostClasspath(hostClasspaths, "tools.jar", Regex("tools.jar"))
276+
else
277+
null
274278
}
275279

276280
// Directory for input source files

src/main/kotlin/com/tschuchort/compiletesting/Utils.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,6 @@ internal inline fun <R> captureSystemOut(crossinline f: () -> R): Pair<R, String
9797
val systemOutBuffer = Buffer()
9898
val ret = withSystemOut(PrintStream(systemOutBuffer.outputStream()), f)
9999
return Pair(ret, systemOutBuffer.readString(Charset.defaultCharset()))
100-
}
100+
}
101+
102+
internal fun File.existsOrNull(): File? = if (exists()) this else null

src/test/kotlin/com/tschuchort/compiletesting/KotlinCompilationTests.kt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -630,12 +630,6 @@ class KotlinCompilationTests {
630630
private fun defaultCompilerConfig(): KotlinCompilation {
631631
return KotlinCompilation().apply {
632632
workingDir = temporaryFolder.root
633-
634-
toolsJar = if(isJdk9OrLater())
635-
null
636-
else
637-
jdkHome!!.resolve("lib\\tools.jar")
638-
639633
inheritClassPath = false
640634
skipRuntimeVersionCheck = true
641635
correctErrorTypes = true

0 commit comments

Comments
 (0)