Getting Started

This page provides a brief intro to the ktpack command line tool. We will create a new project, build the source code, and run the compiled program.

Create a new project with the new command:

$ ktpack new hello_world

By default, the --bin flag is used to create a binary program. Alternatively the --lib flag can be used to create a library. The command generated two files:

$ cd hello_world
$ tree .
.
├── pack.toml
└── src
    └── common
        └── kotlin
            └── main.kt

1 directory, 2 files

This is a basic, yet fully functional Ktpack project. The project is described in pack.toml:

[module]
name = "hello_world"
version = "1.0.0"
kotlinVersion = "1.9.24"

The pack.toml Manifest contains all the metadata required to operate a Ktpack project. In src/common/kotlin/main.kt we have this program:

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

The source file can be compiled into a binary with the build command:

$ ktpack build

Compiling hello_world v1.0.0 (/users/developer/hello_world)

To run the executable we compiled:

$ ./out/linux_x64/debug/bin/hello_world.kexe
Hello, world!

Note: the linux_x64 directory could also be macosx_[x64|arm64] or mingw_x64 based on your operating system

Alternatively we can use the run command:

$ ktpack run

Compiling hello_world v1.0.0 (/users/developer/hello_world)
Running 'out/linux_x64/debug/bin/hello_world.exe'
Hello, World!

To build or run our program for a different target, use the --target or -t option with the build or run commands:

$ ktpack run --target jvm

Compiling hello_world v1.0.0 (/users/developer/hello_world)
Running 'out/jvm/debug/bin/hello_world.jar'
Hello, World!
$ ktpack run --target js_node

Compiling hello_world v1.0.0 (/users/developer/hello_world)
Running 'out/js_node/debug/bin/hello_world.js'
Hello, World!

For the js_browser target, an HTTP Server is started which provides your program at the URL:

$ ktpack run --target js_browser

Compiling hello_world v1.0.0 (/users/developer/hello_world)
Running 'out/js_browser/debug/bin/hello_world.js'
Http Server Available at http://localhost:9543