Your First Program
Now that you've set up your starter Claro project in the previous section, let's go through the process of implementing your first program!
Create hello_world.claro
Fig 1:
print("Hello, world!");
Hello World is a one-liner in Claro, so it's a great place to start learning how to declare a new Claro program using
Bazel. Just to keep things simple, copy the above line into a new file at //example/hello_world.claro
.
Fig 2:
demo
|-- .bazelrc
|-- .bazelversion
|-- MODULE.bazel
|-- README.md
`-- example
|-- BUILD
|-- demo.claro
|-- hello_world.claro
`-- input.txt
1 directory, 8 files
Declare a New claro_binary(...)
Target in Your BUILD File
Now, we'll simply add a new build target for our Hello World program to the existing BUILD file that was generated as part of the starter project.
Fig 3:
load("@claro-lang//:rules.bzl", "claro_binary")
claro_binary(
name = "demo_bin",
main_file = "demo.claro",
resources = {
"Input": "input.txt",
}
)
claro_binary(
name = "hello_world",
main_file = "hello_world.claro",
)
Now Execute Your Program!
That's all there is to it! Now you can use the following command to have Bazel build and then run your program:
Note: The below recording was made with asciinema - try pausing and copying any text.
Congratulations! You just wrote and executed your first Claro program entirely from scratch!
Avoiding Bazel's Extra Output
Notice that when you used bazel run ...
to run your executable build target, Bazel produced a bunch of INFO: ...
logs related to the build process. Since the program built successfully, this is something that you can usually just
ignore. However, if this extra noise bothers you, you can make use of Bazel's generated build artifacts to run the
program directly, without any of Bazel's extra logging. Notice the very last line in Bazel's output:
INFO: Running command line: bazel-bin/example/hello_world
This is a script that can be directly invoked to run the built executable program locally.
Note: The below recording was made with asciinema - try pausing and copying any text.
Generating a Portable Executable ("Deploy Jar")
As Claro is a JVM language, you can easily generate a self-contained Jar file that can be run anywhere that a JVM is
installed. Generate the "Deploy Jar" by appending _deploy.jar
to the end of any claro_binary()
build target, and can
then run it using java -jar ...
as you would any executable Jar:
Note: The below recording was made with asciinema - try pausing and copying any text.