# Adding Kotlin to a Java project – tutorial Kotlin is fully interoperable with Java, so you can gradually introduce it into your existing Java projects without having to rewrite everything. In this tutorial, you'll learn how to: * Set up Maven or Gradle build tools to compile both Java and Kotlin code. * Organize Java and Kotlin source files in your project directories. * Convert Java files to Kotlin using IntelliJ IDEA. Tip: You can use any existing Java project for this tutorial or clone our public [sample project](https://github.com/kotlin-hands-on/kotlin-junit-sample/tree/main/complete) with both Maven and Gradle build files already set up. You can also hand over the conversion to your AI agent of choice using our [prepared skill](https://github.com/Kotlin/kotlin-agent-skills/blob/main/skills/kotlin-tooling-java-to-kotlin/SKILL.md). Keep in mind that AI processing results are not entirely predictable. ## Project configuration To add Kotlin to a Java project, you need to configure the project to use both Kotlin and Java, depending on the build tool you use. The project configuration ensures that both Kotlin and Java code are compiled properly and can reference each other seamlessly. ### Maven Note: Starting with IntelliJ IDEA 2025.3, when you add your first Kotlin file to a Maven-based Java project, the IDE automatically updates your `pom.xml` file to include the Kotlin Maven plugin and standard dependencies. You can still configure it manually if you want to customize versions or build phases. To use Kotlin and Java together in a Maven project, apply the Kotlin Maven plugin and add Kotlin dependencies in your `pom.xml` file: 1. In the `` section, add the Kotlin version property: ```XML 2.4.21 ``` 2. In the `` section, add the required dependencies to the `` section: ```XML org.junit.jupiter junit-jupiter-engine test org.junit.jupiter junit-jupiter-params test ``` 3. In the `` section, add the Kotlin plugin: ```XML org.jetbrains.kotlin kotlin-maven-plugin ${kotlin.version} true ``` Enabling `true` in the Kotlin Maven plugin helps to: * Automatically add the `kotlin-stdlib` dependency to the project. * Configure execution phases to compile Kotlin first, then Java. * Reference Kotlin code in Java code and vice versa. * Automatically align the JVM target version with the Java compiler version. You don't need a separate `maven-compiler-plugin` in the `` section when using the Kotlin Maven plugin with extensions. 4. Reload the Maven project in your IDE. 5. Run tests to verify the configuration: ```BASH ./mvnw clean test ``` ### Gradle To use Kotlin and Java together in a Gradle project, apply the Kotlin JVM plugin and add Kotlin dependencies in your `build.gradle.kts` file: 1. In the `plugins {}` block, add the Kotlin JVM plugin: ```KOTLIN plugins { // Other plugins kotlin("jvm") version "2.4.21" } ``` 2. Set the JVM toolchain version to match your Java version: ```KOTLIN kotlin { jvmToolchain(17) } ``` This ensures Kotlin uses the same JDK version as your Java code. 3. In the `dependencies {}` block, add the `kotlin("test")` library that provides Kotlin test utilities and integrates with JUnit: ```KOTLIN dependencies { // Other dependencies testImplementation(kotlin("test")) // Other test dependencies } ``` 4. Reload the Gradle project in your IDE. 5. Run your tests to verify the configuration: ```BASH ./gradlew clean test ``` ## Project structure With this configuration, you can mix Java and Kotlin files in the same source directories: ``` src/ ├── main/ │ ├── java/ # Java and Kotlin production code │ └── kotlin/ # Additional Kotlin production code (optional) └── test/ ├── java/ # Java and Kotlin test code └── kotlin/ # Additional Kotlin test code (optional) ``` You can create these directories manually or let IntelliJ IDEA create them when you add your first Kotlin file. The Kotlin plugin automatically recognizes both `src/main/java` and `src/test/java` directories, so you can keep `.kt` and `.java` files in the same directories. ## Convert Java files to Kotlin The Kotlin plugin also bundles a Java to Kotlin converter (J2K) that automatically converts Java files to Kotlin. To use J2K on a file, click Convert Java File to Kotlin File in its context menu or in the Code menu of IntelliJ IDEA. ![Convert Java to Kotlin](images/convert-java-to-kotlin.png) While the converter is not fool-proof, it does a pretty decent job of converting most boilerplate code from Java to Kotlin. However, some manual tweaking is sometimes required. ## Explore compiler plugins If you have a more complex project that uses [Spring](https://spring.io/) or Java Persistence API (JPA), you can use Kotlin compiler plugins that automatically adapt Kotlin's language features to framework expectations, reducing boilerplate: * The [all-open](all-open-plugin.html) plugin automatically makes classes and their members `open` when used with specific annotations. This is particularly useful for frameworks like Spring that require classes to be non-final. For Spring, you can use a dedicated [kotlin-spring](kotlin-spring-plugin.html) plugin, which is a wrapper on top of `all-open`. It specifies Spring annotations automatically. * The [no-arg](no-arg-plugin.html) plugin generates an additional zero-argument constructor for classes with specific annotations. This allows JPA to instantiate classes that otherwise wouldn't have a default constructor. For JPA, you can use the [kotlin-jpa](kotlin-jpa-plugin.html) plugin, which wraps both `no-arg` and `all-open`. It specifies JPA annotations automatically, so entities become `open` and get a zero-argument constructor. * The [power-assert](power-assert.html) plugin improves the debugging experience by providing detailed failure messages with contextual information for assertions. It shows intermediate values and helps you understand why a test failed. ## Next step The easiest way to start using Kotlin in a Java project is by adding Kotlin tests first: [Add your first Kotlin test to your Java project](jvm-test-using-junit.html) ### See also * [Kotlin and Java interoperability details](java-to-kotlin-interop.html) * [Maven build configuration reference](maven.html)