Skip to content

Commit b689dfb

Browse files
committed
#3 Find jdkHome automatically by default
1 parent 9272a2b commit b689dfb

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

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

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,6 @@ class KotlinCompilation {
6666
/** Annotation processors to be passed to kapt */
6767
var annotationProcessors: List<Processor> = emptyList()
6868

69-
/**
70-
* Path to the JDK to be used
71-
*
72-
* If null, no JDK will be used with kotlinc (option -no-jdk)
73-
* and the system java compiler will be used with empty bootclasspath
74-
* (on JDK8) or --system none (on JDK9+). This can be useful if all
75-
* the JDK classes you need are already on the (inherited) classpath.
76-
* */
77-
var jdkHome: File? = null
78-
7969
/**
8070
* Helpful information (if [verbose] = true) and the compiler
8171
* system output will be written to this stream
@@ -212,6 +202,16 @@ class KotlinCompilation {
212202
/** Paths to output directories for friend modules (whose internals should be visible) */
213203
var friendPaths: MutableList<File> = mutableListOf()
214204

205+
/**
206+
* Path to the JDK to be used
207+
*
208+
* If null, no JDK will be used with kotlinc (option -no-jdk)
209+
* and the system java compiler will be used with empty bootclasspath
210+
* (on JDK8) or --system none (on JDK9+). This can be useful if all
211+
* the JDK classes you need are already on the (inherited) classpath.
212+
* */
213+
var jdkHome: File? = if(inheritClassPath) null else getJdkHome()
214+
215215
/**
216216
* Path to the kotlin-stdlib.jar
217217
* If none is given, it will be searched for in the host
@@ -466,7 +466,8 @@ class KotlinCompilation {
466466
val resourcesUri = URI.create(
467467
this::class.java.classLoader
468468
.getResource("META-INF/services/org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar")
469-
.toString().removeSuffix("/META-INF/services/org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar")
469+
?.toString()?.removeSuffix("/META-INF/services/org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar")
470+
?: throw AssertionError("Could not get path to ComponentRegistrar service from META-INF")
470471
)
471472

472473
val resourcesPath = when(resourcesUri.scheme) {
@@ -716,13 +717,20 @@ class KotlinCompilation {
716717
private fun searchSystemOutForKnownErrors(compilerSystemOut: String) {
717718
if(compilerSystemOut.contains("No enum constant com.sun.tools.javac.main.Option.BOOT_CLASS_PATH")) {
718719
warn(
719-
"${this::class.simpleName} has detected that the compilation failed with an error that may be " +
720+
"${this::class.simpleName} has detected that the compiler output contains an error message that may be " +
720721
"caused by including a tools.jar file together with a JDK of version 9 or later. " +
721722
if (inheritClassPath)
722723
"Make sure that no tools.jar (or unwanted JDK) is in the inherited classpath"
723724
else ""
724725
)
725726
}
727+
728+
if(compilerSystemOut.contains("Unable to find package java.")) {
729+
warn (
730+
"${this::class.simpleName} has detected that the compiler output contains an error message " +
731+
"that may be caused by a missing JDK. This can happen if jdkHome=null and inheritClassPath=false."
732+
)
733+
}
726734
}
727735

728736
/** Tries to find a file matching the given [regex] in the host process' classpath */

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,6 @@ class KotlinCompilationTests {
630630
private fun defaultCompilerConfig(): KotlinCompilation {
631631
return KotlinCompilation().apply {
632632
workingDir = temporaryFolder.root
633-
jdkHome = getJdkHome()
634633

635634
toolsJar = if(isJdk9OrLater())
636635
null

0 commit comments

Comments
 (0)