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
- Step 1: Create a Kotlin .kt File
- Step 2: Create a build.gradle File (for Gradle Users)
- Step 2: Create a pom.xml File (for Maven Users)
- Step 3: Compile and Package Your Code
- Step 4: Create a Fat JAR
- Step 5: Convert the JAR to an Executable File
- Step 6: Run Your Executable File
- Troubleshooting
- Conclusion
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!
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 |