# Java and Kotlin with Elide

> Run Java and Kotlin source files with Elide, use the bundled JDK 25 and Kotlin tools, resolve Maven dependencies, and build and test a project.

Elide compiles, formats, builds, and tests Java (up to JDK 25) and Kotlin (2.5.0-Beta2), and resolves Maven
dependencies.

> [!TIP] No JDK to install
> The JDK and Kotlin tools ship with Elide; no separate JDK, Kotlin compiler or build tool is needed.

## Run your first program

[Install Elide](https://elide.help/docs/installation), then choose a template below. Each creates a new project directory with a program
and a test.

### Kotlin

```bash
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

```bash
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:

```text
Hello Elide!
```

Run the generated test with `elide test`. To use a library, declare its
[Maven dependency](https://elide.help/docs/toolchain/jvm/maven) in `elide.pkl`, then:

```bash
elide install
elide test
elide run
```

Already have a project? [Keep Maven/Gradle](https://elide.help/docs/build-tools) and use Elide's compiler or JDK, or
[migrate your build](https://elide.help/docs/toolchain/usage/adopt) to `elide.pkl`.

## Run a source file

A file with a `main` entrypoint compiles and runs directly:

```bash
elide run Hello.java
elide run Hello.kt
```

## Use individual tools

`elide java`, `elide javac`, `elide kotlinc`, and `elide jar` work like the tools they name. Arguments go after `--`:

```bash
elide javac -- -d target src/main/java/app/Main.java
elide kotlinc -- -d target src/main/kotlin/app/Main.kt
elide java -- -cp target app.Main
```

`elide format` formats Java with Google Java Format and Kotlin with ktfmt:

```bash
elide format -- src                          # rewrite files
elide format --list-files -- --dry-run src   # list files that would change; exit 1 if any
```

## Start a project

```bash
elide init --plain --skip-defaults --template java
```

| Template | Creates |
| --- | --- |
| `java` | A Java project. |
| `ktjvm` | A Kotlin project. |
| `workspace` | A Kotlin library and an app that depends on it. |

To migrate a Maven or Gradle project, `elide adopt` runs an AI coding agent; see
[Adopt an Existing Project](https://elide.help/docs/toolchain/usage/adopt).

## Build a project

```bash
elide install   # resolve Maven dependencies
elide build     # compile and assemble artifacts
elide test      # run JUnit tests
elide run       # run the project's main class
```

<img src="https://elide.help/assets/demos/project-build.gif" alt="Elide projects — build, run, and reuse cached work" width="1591" height="910" loading="lazy" decoding="async" class="rounded-lg border border-border my-6 w-full" />

`elide build --inspect` lists the project's tasks. Manifest: [Project setup](https://elide.help/docs/projects). Compiler and packaging
options: [JVM Toolchain](https://elide.help/docs/toolchain/jvm).

## What programs can use

The JDK, the Kotlin standard library, bundled kotlinx libraries, JUnit for tests, and Elide's annotations; there is no
Elide runtime API and no calls into JavaScript or Python. See [JVM APIs](https://elide.help/docs/api-jvm). For a database, add its JDBC driver
as a Maven dependency ([Databases](https://elide.help/docs/api-database#other-languages)); to share a library, see
[Maven publishing](https://elide.help/docs/toolchain/jvm/publishing).

## Tracing and metrics

Java and Kotlin programs that Elide runs get no OpenTelemetry API, agent, or trace context from Elide. Add the
OpenTelemetry Java SDK or agent to the program as on any JVM. `elide build` and `elide test` still export their own
spans; see [OpenTelemetry](https://elide.help/docs/opentelemetry#languages).

## Rewrite and inspect code

For projects with `elide.pkl`, [OpenRewrite](https://elide.help/docs/toolchain/jvm/rewrite) previews and applies Java recipes, and the
[dependency code index](https://elide.help/docs/toolchain/jvm/code-index) finds class origins, inspects members, and queries compiled
Java and Kotlin references. Both are experimental in 1.6.0.

## Samples

- [Java records and streams](https://elide.help/docs/code-samples#java-records-and-streams)
- [Kotlin collections](https://elide.help/docs/code-samples#kotlin-collections)
- [Kotlin tests](https://elide.help/docs/code-samples#kotlin-tests)
- [Kotlin coroutines](https://elide.help/docs/code-samples#kotlin-coroutines)
- [Java with a Maven dependency](https://elide.help/docs/code-samples#java-with-a-maven-dependency)

Start from a template:

```bash
elide init --template java        # Java app with a JUnit test
elide init --template ktjvm       # Kotlin app with a test
elide init --template workspace   # Kotlin library and app in one workspace
```
