How do I make a CLI .exe from a Kotlin .kt file? A Step-by-Step Guide
Image by Roch - hkhazo.biz.id

How do I make a CLI .exe from a Kotlin .kt file? A Step-by-Step Guide

Posted on

Are you tired of running your Kotlin scripts from an IDE or through the command line? Do you want to create a standalone executable file that can be distributed and run by anyone, regardless of their dev environment? Look no further! In this article, we’ll take you by the hand and walk you through the process of creating a CLI .exe from a Kotlin .kt file.

What You’ll Need

To follow along with this tutorial, you’ll need the following:

  • Kotlin installed on your machine (version 1.5 or higher)
  • Java Development Kit (JDK) installed on your machine (version 11 or higher)
  • Apache Maven or Gradle build tool installed on your machine
  • A Kotlin .kt file with a main function (we’ll get to that later)

Step 1: Create a Kotlin .kt File

Let’s start with the basics. Create a new Kotlin file called `Main.kt` with the following code:


fun main() {
    println("Hello, World!")
}

This file contains a single main function that prints “Hello, World!” to the console.

Step 2: Create a build.gradle File (for Gradle Users)

If you’re using Gradle as your build tool, create a new file called `build.gradle` in the same directory as your `Main.kt` file:


plugins {
    id 'org.jetbrains.kotlin.jvm' version '1.5.31'
    id 'application'
}

group 'com.example'
version '1.0'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.5.31'
}

application {
    mainClass = 'MainKt'
}

jar {
    manifest {
        attributes 'Main-Class': 'MainKt'
    }
}

This file tells Gradle to compile our Kotlin code, include the necessary dependencies, and specify the main class.

Step 2: Create a pom.xml File (for Maven Users)

If you’re using Maven as your build tool, create a new file called `pom.xml` in the same directory as your `Main.kt` file:


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>cli-exe</artifactId>
    <version>1.0</version>

    <properties>
        <kotlin.version>1.5.31</kotlin.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-stdlib-jdk8</artifactId>
            <version>${kotlin.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-maven-plugin</artifactId>
                <version>${kotlin.version}</version>
                <configuration>
                    <compilerArguments>
                        <argument>-verbose</argument>
                    </compilerArguments>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.2.0</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>MainKt</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

This file tells Maven to compile our Kotlin code, include the necessary dependencies, and specify the main class.

Step 3: Compile and Package Your Code

Now that we have our build files set up, let’s compile and package our code:


// For Gradle users:
gradle build

// For Maven users:
mvn package

This will create a `Main.jar` file in your project directory.

Step 4: Create a Fat JAR

A fat JAR is a JAR file that includes all the dependencies required to run our application. We’ll use the Shadow plugin to create a fat JAR:


// For Gradle users:
gradle shadowJar

// For Maven users:
mvn assembly:assembly

This will create a `Main-fat.jar` file in your project directory.

Step 5: Convert the JAR to an Executable File

We’ll use a tool called Launch4j to convert our JAR file to an executable file. Download and install Launch4j from the official website.

Once installed, create a new configuration file called `launch4j.xml` with the following content:


<?xml version="1.0" encoding="UTF-8"?>
<launch4jConfig>
    <headerType>gui</headerType>
    <jre>
        <minVersion>11</minVersion>
    </jre>
    <outfile>main.exe</outfile>
    <jar>Main-fat.jar</jar>
</launch4jConfig>

This file tells Launch4j to create a GUI executable file called `main.exe` that runs our `Main-fat.jar` file.

Open a command prompt and navigate to the directory where your `launch4j.xml` file is located. Run the following command:


launch4jc launch4j.xml

This will create a `main.exe` file in your directory.

Step 6: Run Your Executable File

Congratulations! You’ve successfully created a CLI executable file from your Kotlin .kt file. Run the `main.exe` file to see the output:


main.exe

You should see “Hello, World!” printed to the console.

Troubleshooting

If you encounter any issues during the process, here are some common troubleshooting steps:

  • Check that your Kotlin and Java versions are compatible.
  • Verify that your build files are correctly configured.
  • Make sure you have the necessary dependencies installed.
  • Try cleaning and rebuilding your project.

Conclusion

Creating a CLI executable file from a Kotlin .kt file is a straightforward process that requires some configuration and packaging. By following the steps outlined in this article, you should be able to create a standalone executable file that can be distributed and run by anyone. Good luck, and happy coding!

Frequently Asked Question

Kotlin developers, rejoice! We’ve got the scoop on how to turn your .kt file into a handy .exe file for a command-line interface (CLI) application.

What’s the first step in creating a CLI .exe from a Kotlin .kt file?

First, you’ll need to compile your Kotlin code into a Java class file using the `kotlinc` compiler. You can do this by running the command `kotlinc your_kotlin_file.kt -include-runtime -d your_jar_file.jar` in your terminal.

How do I package my Java class file into a .exe file?

To create a .exe file, you’ll need to use a tool like Launch4j or GraalVM. Launch4j is a popular choice, and you can use it to create a .exe file by following these steps: create a new configuration, select your .jar file, and then configure the output file to be a .exe file.

What dependencies do I need to include in my project for a CLI application?

For a CLI application, you’ll need to include the Kotlin standard library and any other dependencies specific to your project. You can manage these dependencies using a build tool like Gradle or Maven.

Can I use an IDE to simplify the process of creating a CLI .exe file?

Absolutely! IDEs like IntelliJ IDEA and Eclipse offer built-in tools and plugins to help you create a CLI .exe file from your Kotlin .kt file. They can automate many of the steps involved, making the process much easier.

Are there any specific considerations I should keep in mind when creating a CLI .exe file?

Yes, be mindful of your project’s platform dependencies and ensure that your .exe file is compatible with the target operating system. You should also consider code signing and certifications, especially if you plan to distribute your application widely.

Leave a Reply

Your email address will not be published. Required fields are marked *

Keyword Frequency
“How do I make a CLI .exe from a Kotlin .kt file” 5
Kotlin 10
CLI 5
.exe 5
.kt 5
Gradle 2
Maven