Running ZIO Apps
ZIOAppDefault.
Running ZIO Apps is a free Scala for Backend Engineering & Functional Programming lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Scala for Backend Engineering & Functional Programming learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
From Description to Execution
A ZIO value is only a blueprint. To execute it you hand it to the ZIO runtime, usually by extending ZIOAppDefault rather than calling the runtime by hand.
ZIOAppDefault
Extend ZIOAppDefault and implement run. The framework provides default services (Console, Clock, Random, System) and manages startup and shutdown.
import zio._
object Hello extends ZIOAppDefault {
def run = Console.printLine("Hello, ZIO!")
}The run Value
run is a ZIO whose environment is satisfied by the default services. Its error and success types are flexible; the runtime reports failures and sets the exit code.
A Multi-Step App
Compose the app logic with a for-comprehension. Each line is an effect run in sequence by the runtime.
import zio._
object Greeter extends ZIOAppDefault {
def run = for {
_ <- Console.printLine("What is your name?")
name <- Console.readLine
_ <- Console.printLine(s"Hello, $name!")
} yield ()
}Providing Layers in run
If your app depends on custom services, supply their layers with provide on the run effect so the environment is fully satisfied before execution.
import zio._
object App extends ZIOAppDefault {
// def run = program.provide(MyService.layer)
def run = ZIO.unit
}Exit Codes
By default a successful run exits with code 0 and a failure with a non-zero code. You can also return an explicit ExitCode using exitCode.
import zio._
object App extends ZIOAppDefault {
def run = Console.printLine("done").exitCode
}Command-Line Arguments
Access program arguments with ZIOAppArgs via ZIOApp.args or getArgs, returning a Chunk[String].
import zio._
object App extends ZIOAppDefault {
def run = for {
args <- getArgs
_ <- Console.printLine(s"args: ${args.mkString(", ")}")
} yield ()
}Handling Failures at the Edge
It is good practice to handle or log errors at the top level so the runtime prints a clean message. Use catchAll or tapError in run.
import zio._
object App extends ZIOAppDefault {
val logic = ZIO.fail("oops")
def run = logic.catchAll(e => Console.printLine(s"Failed: $e"))
}Forking Fibers
Inside run you can fork effects onto fibers for concurrency and later join them. The runtime supervises all fibers and interrupts them on shutdown.
import zio._
val prog = for {
fiber <- ZIO.succeed(40).fork
result <- fiber.join
_ <- Console.printLine(s"got $result")
} yield ()Manual Unsafe Run
Outside of ZIOAppDefault you can run an effect with Unsafe.unsafe and a Runtime, but this is reserved for integration boundaries, not normal app code.
import zio._
// Unsafe.unsafe { implicit u =>
// Runtime.default.unsafe.run(myEffect).getOrThrow()
// }Plain Scala main
For comparison, a self-contained Scala entry point. ZIOAppDefault plays the role of main but wires in services, error reporting, and fiber supervision.
object Main {
def main(args: Array[String]): Unit = {
println("Hello, ZIO!")
}
}Quick Check
What is the recommended way to run a ZIO application?
Recap
You learned to run ZIO apps:
ZIOAppDefaultwith aruneffect- default services and layer
provide - exit codes, args, top-level error handling
- forking fibers and unsafe boundaries
You've completed the ZIO Fundamentals course.
Frequently asked questions
Is the “Running ZIO Apps” lesson free?
Yes — the full text of “Running ZIO Apps” is free to read here on the web, and the Scala for Backend Engineering & Functional Programming course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Scala for Backend Engineering & Functional Programming course, upgrade to CoddyKit PRO.
What will I learn in “Running ZIO Apps”?
ZIOAppDefault. You practise Scala for Backend Engineering & Functional Programming with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Scala for Backend Engineering & Functional Programming?
No prior experience is required. Scala for Backend Engineering & Functional Programming on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Running ZIO Apps” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Scala for Backend Engineering & Functional Programming lesson?
Yes. Every Scala for Backend Engineering & Functional Programming lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- The ZIO Effect
- Composing ZIO
- Error and Dependency Channels
- Running ZIO Apps