Java and Kotlin with Elide
Run Java or Kotlin with the bundled JDK and compilers. Create a small app, run its tests, then add Maven dependencies.
On this page
Install Elide, then choose a template below. Each creates a new project directory with a program and a test.
Kotlin
mkdir hello-kotlin
cd hello-kotlin
elide init --plain --skip-defaults --skip-run --template ktjvm
elide run
Edit src/main/com/example/Hello.kt, then run elide run again.
Java
mkdir hello-java
cd hello-java
elide init --plain --skip-defaults --skip-run --template java
elide run
Edit src/main/com/example/Hello.java, then run elide run again.
Both programs print:
Hello Elide!
Run the generated test with elide test. To use a library, declare its Maven dependency in elide.pkl, then:
elide install
elide test
elide run
For an existing project, keep Maven or Gradle and use Elide's compiler or JDK, or migrate the build to elide.pkl.
Call a bundled tool directly for a single task, or use an Elide project to manage dependencies, tests, and artifacts. Both use the same binary.
Java
Compile with javac and format with Google Java Format, both bundled. Tool arguments go after the -- separator.
Compile Java sources
Write class files with standard javac flags, in a script, a CI job, or an existing source tree.
elide javac -- -d target src/main/java/app/Main.javaFormat Java code
Rewrite files in place with the bundled Google Java Format.
elide javaformat -- --replace src/main/java
Kotlin
Call the bundled Kotlin compiler and ktfmt directly, or build Kotlin with Java and Maven dependencies in an Elide project.
Compile Kotlin sources
Compile Kotlin to JVM bytecode with kotlinc options.
elide kotlinc -- -d target src/main/kotlin/app/Main.ktFormat Kotlin code
Format with the bundled ktfmt: the same version locally and in CI.
elide ktfmt -- src/main/kotlin
Project commands
These commands work in Java, Kotlin, and mixed projects. The project manifest declares the source sets, dependencies, entrypoint, and artifacts.
- Resolve Maven dependenciesInstall the packages declared by the project and assemble its JVM classpaths.
elide install - Build a JVM projectCompile configured Java and Kotlin source sets and produce their artifacts.
elide build - Run tests and coverageCompile test sources and execute Java or Kotlin tests with JUnit Platform.
elide test - Package a JARCall the embedded JAR tool directly, or declare a JAR artifact in elide.pkl.
elide jar - Build a containerBuild a container image from a JVM application with the embedded Jib builder.
elide build
This project declares one Java entrypoint, main and test source sets that mix both languages, and three JAR artifacts. Java and Kotlin share each source set's classpath.
amends "elide:project.pkl"
import "elide:Jvm.pkl" as Jvm
import "elide:Sources.pkl" as Sources
name = "order-report"
jvm {
main = "shop.Main"
}
sources {
["main"] = new Sources.SourceSetSpec {
paths {
"src/main/kotlin/**/*.kt"
"src/main/java/**/*.java"
}
}
["test"] = new Sources.SourceSetSpec {
paths {
"src/test/kotlin/**/*.kt"
"src/test/java/**/*.java"
}
}
}
artifacts {
["app"] = new Jvm.Jar {
name = "order-report"
main = "shop.Main"
sources { "main" }
}
["sources"] = new Jvm.SourceJar {
sources { "main" }
}
["docs"] = new Jvm.JavadocJar {
sources { "main" }
}
}
The default build compiles the non-test source sets and produces their artifacts. Tests compile against the main outputs and run on JUnit Platform.
elide project info # Confirm the detected project and entrypoint
elide install # Resolve declared Maven dependencies
elide build --inspect # List generated tasks and task options
elide build # Compile main sources and produce default artifacts
elide test # Compile tests and run them with JUnit Platform
elide run # Build as needed, then launch shop.Main
Add a Kotlin test for the example
package shop
import java.math.BigDecimal
import kotlin.test.Test
import kotlin.test.assertEquals
class OrderTotalsTest {
@Test
fun `summarizes quantities and prices`() {
val summary = OrderTotals.summarize(
listOf(
LineItem("paper", 3, BigDecimal("7.50")),
LineItem("ink", 2, BigDecimal("18.25")),
),
)
assertEquals(5, summary.itemCount)
assertEquals(BigDecimal("59.00"), summary.subtotal)
}
}
elide test runs the full suite. elide test --select=class:shop.OrderTotalsTest runs one class.
The other JVM tools are commands too. Prefix the tool name with elide and put its arguments after --.
Run compiled classes
Launch a JVM entrypoint with the bundled Java runtime.
elide java -- -cp target app.MainGenerate API docs
Generate Javadoc using the standard command-line options.
elide javadoc -- -d docs src/main/java/app/Main.javaInspect bytecode
Disassemble a compiled class.
elide javap -- -classpath target -c app.MainBuild a native executable
Compile a JVM application ahead of time to a native executable.
elide native-image -- -cp app.jar app.Main
Programs run on the bundled JDK with the Kotlin standard library, bundled kotlinx libraries, JUnit for tests and Elide's annotations; see JVM APIs. There is no Elide runtime API, no calls into JavaScript or Python, and no OpenTelemetry API, agent or trace context; add the OpenTelemetry Java SDK yourself, and see OpenTelemetry for the spans elide build and elide test export.
A Kotlin model and calculation share the main source set with a Java entrypoint. Elide compiles both languages together and runs the Java entrypoint.
package shop
import java.math.BigDecimal
data class LineItem(
val sku: String,
val quantity: Int,
val unitPrice: BigDecimal,
)
data class OrderSummary(
val itemCount: Int,
val subtotal: BigDecimal,
)
object OrderTotals {
@JvmStatic
fun summarize(lines: List<LineItem>): OrderSummary = OrderSummary(
itemCount = lines.sumOf { it.quantity },
subtotal = lines.fold(BigDecimal.ZERO) { total, line ->
total + line.unitPrice.multiply(line.quantity.toBigDecimal())
},
)
}
package shop;
import java.math.BigDecimal;
import java.util.List;
public final class Main {
public static void main(String[] args) {
var lines = List.of(
new LineItem("paper", 3, new BigDecimal("7.50")),
new LineItem("ink", 2, new BigDecimal("18.25")),
new LineItem("labels", 4, new BigDecimal("3.10"))
);
var summary = OrderTotals.summarize(lines);
System.out.printf(
"%d items · $%s subtotal%n",
summary.getItemCount(),
summary.getSubtotal()
);
}
}
elide runWas this page helpful?