The Kotlin compiler infrastructure providing JVM, JavaScript, and metadata compilation capabilities
—
Complete JVM bytecode compilation supporting all Kotlin language features, Java interoperability, and advanced optimization passes. The JVM compiler produces bytecode compatible with Java 8+ and supports both standalone and incremental compilation modes.
Main entry point for JVM compilation, handling the complete compilation pipeline from source analysis to bytecode generation.
/**
* Kotlin to JVM bytecode compiler implementation
* Supports K2 (new) compiler pipeline with enhanced performance and analysis
*/
class K2JVMCompiler : CLICompiler<K2JVMCompilerArguments>() {
override val platform: TargetPlatform // JvmPlatforms.defaultJvmPlatform
override fun createArguments(): K2JVMCompilerArguments
override fun doExecute(
arguments: K2JVMCompilerArguments,
configuration: CompilerConfiguration,
rootDisposable: Disposable,
paths: KotlinPaths?
): ExitCode
override fun doExecutePhased(
arguments: K2JVMCompilerArguments,
services: Services,
messageCollector: MessageCollector
): ExitCode
}Comprehensive argument configuration for JVM compilation including output control, classpath management, and optimization settings.
/**
* Arguments specific to JVM compilation
* Extends CommonCompilerArguments with JVM-specific options
*/
class K2JVMCompilerArguments : CommonCompilerArguments() {
/** Output directory for compiled .class files */
var destination: String?
/** Output JAR file path */
var jar: String?
/** Classpath for compilation (directories and JAR files) */
var classpath: String?
/** Include Kotlin runtime in the resulting JAR */
var includeRuntime: Boolean
/** Path to JDK home directory */
var jdkHome: String?
/** Target JVM version (1.8, 9, 10, 11, 15, 17, 18, 19, 20, 21) */
var jvmTarget: String?
/** Module name for the compilation */
var moduleName: String?
/** Disable standard script definitions */
var disableStandardScript: Boolean
/** Enable script mode compilation */
var script: Boolean
/** Compile and execute script expression */
var expression: String?
/** Enable REPL mode */
var repl: Boolean
/** Build file for module compilation */
var buildFile: String?
/** Java module path */
var javaModulePath: String?
/** Add modules to compile */
var addModules: String?
/** No JDK class path roots */
var noJdk: Boolean
/** No standard library */
var noStdlib: Boolean
/** No reflection */
var noReflect: Boolean
/** Emit JVM debug information */
var jvmDebug: Boolean
/** Use old class file format with JVM target */
var useOldClassFiles: Boolean
}JVM-specific configuration keys for fine-grained compilation control.
object JVMConfigurationKeys {
/** Output directory for .class files */
val OUTPUT_DIRECTORY: CompilerConfigurationKey<File>
/** Output JAR file */
val OUTPUT_JAR: CompilerConfigurationKey<File>
/** Classpath entries (directories and JARs) */
val CLASSPATH_ROOTS: CompilerConfigurationKey<List<File>>
/** Include Kotlin runtime in output */
val INCLUDE_RUNTIME: CompilerConfigurationKey<Boolean>
/** JDK home directory */
val JDK_HOME: CompilerConfigurationKey<File>
/** JVM target version */
val JVM_TARGET: CompilerConfigurationKey<JvmTarget>
/** Disable standard script definition */
val DISABLE_STANDARD_SCRIPT_DEFINITION: CompilerConfigurationKey<Boolean>
/** Retain output in memory */
val RETAIN_OUTPUT_IN_MEMORY: CompilerConfigurationKey<Boolean>
/** Disable optimization */
val DISABLE_OPTIMIZATION: CompilerConfigurationKey<Boolean>
}Supported JVM bytecode target versions with corresponding class file format versions.
enum class JvmTarget(
val majorVersion: Int,
val description: String
) {
JVM_1_8(52, "1.8"),
JVM_9(53, "9"),
JVM_10(54, "10"),
JVM_11(55, "11"),
JVM_15(59, "15"),
JVM_17(61, "17"),
JVM_18(62, "18"),
JVM_19(63, "19"),
JVM_20(64, "20"),
JVM_21(65, "21");
companion object {
val DEFAULT: JvmTarget
fun fromString(string: String): JvmTarget?
}
}Core bytecode compilation functionality transforming Kotlin IR to JVM bytecode.
/**
* Main bytecode compilation entry point
* Handles the complete pipeline from resolved program to bytecode
*/
object KotlinToJVMBytecodeCompiler {
/**
* Compile Kotlin sources to JVM bytecode
*
* @param environment Kotlin compilation environment
* @param configuration Compiler configuration
* @param chunk Module chunk to compile
* @return Compilation result with generated files
*/
fun compileModules(
environment: KotlinCoreEnvironment,
configuration: CompilerConfiguration,
chunk: ModuleChunk
): Boolean
/**
* Generate class files from analysis result
*
* @param environment Compilation environment
* @param analysisResult Frontend analysis result
* @param files Source files to compile
* @return Success status
*/
fun generateClassFiles(
environment: KotlinCoreEnvironment,
analysisResult: AnalysisResult,
files: Collection<KtFile>
): Boolean
}import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler
import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments
import org.jetbrains.kotlin.cli.common.ExitCode
// Configure compilation arguments
val args = K2JVMCompilerArguments().apply {
// Source files to compile
freeArgs = listOf(
"src/main/kotlin/Main.kt",
"src/main/kotlin/Utils.kt"
)
// Output directory
destination = "build/classes/kotlin/main"
// Classpath including dependencies
classpath = listOf(
"lib/kotlin-stdlib-1.9.0.jar",
"lib/my-dependency.jar"
).joinToString(File.pathSeparator)
// JVM target version
jvmTarget = "11"
// Module name
moduleName = "my-app"
}
// Create and execute compiler
val compiler = K2JVMCompiler()
val exitCode = compiler.exec(System.err, Services.EMPTY, args)
when (exitCode) {
ExitCode.OK -> println("Compilation successful")
ExitCode.COMPILATION_ERROR -> println("Compilation failed")
else -> println("Internal error: $exitCode")
}val args = K2JVMCompilerArguments().apply {
freeArgs = listOf("src/")
jar = "build/libs/my-app.jar"
includeRuntime = true
jvmTarget = "1.8"
// Include manifest
manifestMain = "com.example.MainKt"
}
val compiler = K2JVMCompiler()
val result = compiler.exec(System.err, Services.EMPTY, args)val args = K2JVMCompilerArguments().apply {
script = true
freeArgs = listOf("script.kts")
classpath = "lib/kotlin-stdlib.jar"
}
val compiler = K2JVMCompiler()
compiler.exec(System.err, Services.EMPTY, args)import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.config.JVMConfigurationKeys
import org.jetbrains.kotlin.config.CommonConfigurationKeys
import java.io.File
// Create configuration
val configuration = CompilerConfiguration().apply {
put(JVMConfigurationKeys.OUTPUT_DIRECTORY, File("build/classes"))
put(JVMConfigurationKeys.JVM_TARGET, JvmTarget.JVM_11)
put(CommonConfigurationKeys.MODULE_NAME, "my-module")
put(JVMConfigurationKeys.CLASSPATH_ROOTS, listOf(
File("lib/kotlin-stdlib.jar"),
File("lib/dependencies.jar")
))
}
// Create environment and compile
val environment = KotlinCoreEnvironment.createForProduction(
parentDisposable = Disposer.newDisposable(),
configuration = configuration,
configFiles = EnvironmentConfigFiles.JVM_CONFIG_FILES
)
val success = KotlinToJVMBytecodeCompiler.compileModules(
environment = environment,
configuration = configuration,
chunk = ModuleChunk(listOf(module))
)class CompilationException(message: String, cause: Throwable? = null) : RuntimeException(message, cause)
class CompileEnvironmentException(message: String) : Exception(message)Common compilation errors and their handling:
Install with Tessl CLI
npx tessl i tessl/maven-org-jetbrains-kotlin--kotlin-compiler