How to Convert a Kotlin Source File to a Java File

Kailash Vaviya Feb 02, 2024
  1. Why Convert Kotlin to Java
  2. Convert Kotlin Source File to Java Source File
  3. Convert Kotlin Code to Java Using Android Studio/IntelliJ IDEA
  4. Convert Kotlin Code to Java Code Using FernFlower
How to Convert a Kotlin Source File to a Java File

Google declared Kotlin as the preferred language for Android app development. But that does not mean we would not require to use Java.

Kotlin’s success is because of interoperability with Java as both use Java Virtual Machine (JVM).

Hence, converting a Kotlin source code file to a Java one is essential.

Why Convert Kotlin to Java

We may need to convert our Kotlin source file to Java for the following reasons:

  • Integrate some Java features.
  • Compile and run in Java to detect bugs or other issues.
  • To use Java-specific features like operator overloading and static methods.
  • For better code readability.

Convert Kotlin Source File to Java Source File

Since Kotlin and Java use JVM, converting one language’s file to another is possible. There are two ways to convert Kotlin code to Java code; Android Studio/IntelliJ IDEA or FernFlower.

Convert Kotlin Code to Java Using Android Studio/IntelliJ IDEA

Converting a Kotlin file to a Java file consists of two steps:

  • Compile Kotlin file to JVM bytecode.
  • Decompile JVM bytecode to Java file.

Convert a Kotlin file to a Java file

Here’s a step-by-step demonstration for converting a Kotlin file to a Java file using Android Studio.

  • Open the Kotlin file that should be converted to Java.
  • Navigate to Tools > Kotlin > Show Kotlin Bytecode.

converting a Kotlin file to a Java file using Android Studio

  • Open the Kotlin file’s bytecode.
  • Checkmark JVM 8 Target and click on the Decompile button to get the Java code.

Click Decompile button to get the Java code

The process to convert Kotlin to Java in IntelliJ IDEA is the same.

Convert Kotlin Code to Java Code Using FernFlower

The IntelliJ decompile option uses the FernFlower to generate the back-end code.

Hence, if we don’t want to use IntelliJ IDEA or Android Studio for conversion, we can directly use FernFlower. We can do this by following the steps below:

  • Compile the Kotlin source file using the kotlinc file_name.kt command to create the class file.
  • Use FernFlower to get the fernflower.jar file (To get the jar file, run the Gradle build after downloading the FernFlower project).
  • Use the command java -jar fernflower.jar file_name.class to get the converted Java source file.

Although we can use both methods to convert a Kotlin file to a Java file, it is recommended to use IntelliJ IDEA or Android Studio.

The reason is that directly using the FernFlower will lead to a complicated converted code, which will be hard to read and understand.

Kailash Vaviya avatar Kailash Vaviya avatar

Kailash Vaviya is a freelance writer who started writing in 2019 and has never stopped since then as he fell in love with it. He has a soft corner for technology and likes to read, learn, and write about it. His content is focused on providing information to help build a brand presence and gain engagement.

LinkedIn

Related Article - Kotlin Java