These are the code examples from the book Atomic Kotlin, created by an automatic extraction process directly from the book. This repository provides a test of the code that is shown in the book, to verify that it is correct.
Although you can download this repository and run the build system, the examples are more conveniently available pre-integrated with the Intellij IDEA development environment, along with the exercises and solutions for the book. See AtomicKotlin.com for the IDEA examples.
Before you can run the examples from this repository, you must install:
-
The current version of Java, although some earlier versions should also work.
-
The latest version (1.3 or higher) of the Kotlin command-line compiler.
If you just want to download and check the code, Download
Here
and unzip into your destination directory. Open a shell/command window and
move into the root of that directory. You'll know you are in the right
directory if you see the files gradlew
and gradlew.bat
.
On Mac/Linux, enter:
./gradlew run
(If you get a Permission denied error, run chmod +x ./gradlew
)
On Windows, enter
gradlew run
You'll need an Internet connection the first time you compile the code, because Gradle needs to first install itself, then all the support libraries. Once these are installed you can perform additional compiling and running offline.
If all goes well, the examples will run. Everything should complete without errors.
All the book examples are in the subdirectory Examples
in subdirectories
corresponding to the atom names.
To compile and run examples using the Kotlin command-line tools, see Command-Line Hello World.
Follow the tutorials Here.
If you are not familiar with the command line, first read Command-Line Basics.
Kotlin runs on top of Java, so you must first install Java (you only need basic Java; the development kit also works but is not required).
-
Follow the instructions at this link to Install Chocolatey.
-
At a shell prompt, type:
choco install jdk8
(you may also select a more recent version). This takes some time, but when it's finished Java is installed and the necessary environment variables are set.
The Mac comes with a much older version of Java that won't work for the examples in this book, so you'll need to update it to (at least) Java 8.
-
Follow the instructions at this link to Install HomeBrew
-
At a shell prompt, first type
brew update
. When that completes, enterbrew cask install java
.
NOTE: Sometimes the default version of Java that you get with the above installation will be too recent and not validated by the Mac's security system. If this happens you'll either need to turn off the security by hand or install an earlier version of Java. For either choice, you'll need to Google for answers on how to solve the problem (often the easiest approach is to just search for the error message produced by the Mac).
Use the standard package installer with the following shell commands:
Ubuntu/Debian:
-
sudo apt-get update
-
sudo apt-get install default-jdk
Fedora/Redhat:
su -c "yum install java-1.8.0-openjdk"
Open a new shell and type:
java -version
You should see something like the following (Version numbers and actual text will vary):
java version "10.0.2" 2018-07-17
Java(TM) SE Runtime Environment 18.3 (build 10.0.2+13)
Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10.0.2+13, mixed mode)
If you see a message that the command is not found or not recognized, review the installation instructions. If you still can't get it to work, check StackOverflow.
Once you have Java installed, the process to install and run the book examples is the same for all platforms:
-
Download the book examples from the GitHub Repository.
-
unzip
the downloaded file into the directory of your choice. -
Use the Windows Explorer, the Mac Finder, or Nautilus or equivalent on Linux to browse to the directory where you uzipped
AtomicKotlinExamples
, and open a shell there. -
If you're in the right directory, you should see files named
gradlew
andgradlew.bat
in that directory, along with numerous other files and directories. The directories correspond to the chapters in the book. -
At the shell prompt, type
gradlew run
(Windows) or./gradlew run
(Mac/Linux).
The first time you do this, Gradle will install itself and numerous other packages, so it will take some time. After everything is installed, subsequent builds and runs will be much faster.
Note that you must be connected to the Internet the first time you run gradlew
so that Gradle can download the necessary packages.
In each atom-named subdirectory under the Examples
directory, you will see
two files: run.bat
and run.sh
. These are convenience commands for Windows
and Mac/Linux that allow you to compile and run the examples in that
subdirectory. They are not necessary to build the code, but if you begin
experimenting with individual examples they are helpful.
To run these scripts, you must first install a recent version of Python 3.
If you just type run
(./run.sh
in Mac/Linux), it will (using
Gradle) compile and run all the examples in the directory. You can also type
run AnExample.kt
to compile and run the example AnExample.kt
(assuming it
has a main()
).
If you get a Permission denied error on OSX or Linux, run chmod +x ./run.sh
The Kotlin interpreter is also called the REPL (for Read-Evaluate-Print- Loop). To use this you must first install the command-line version of Kotlin.
NOTE: You do not need to install command-line Kotlin for the operations described previously in this README.
In this book, we use Kotlin version 1.3, the latest available at the time. The detailed installation instructions for the command-line compiler are available at The Kotlin Site.
To check your installation, open a new shell and type:
kotlin -version
at the shell prompt. You'll see the version information for your Kotlin installation.
You get the REPL when you type kotlinc
by itself on the command line. You
should see something like the following:
Welcome to Kotlin version 1.3 (JRE 1.8.0_144-b01)
Type :help for help, :quit for quit
>>>
The exact version numbers will vary depending on the versions of Kotlin and Java you've installed, but make sure that you're running Kotlin 1.3 or greater.
The REPL gives you immediate interactive feedback, which is helpful for experimentation. For example, you can do arithmetic:
>>> 42 * 11.3
474.6
Find out more by typing :help
at the Kotlin prompt. To exit the REPL,
type:
>>> :quit
To compile and run examples using the Kotlin command-line tools, see Command-Line Hello World.
The test system is built in so that we (the authors) can verify the correctness of what goes into the book. You don't need to run the tests, but if you want to:
-
gradlew GenerateTests
generates tests from the sources in this repository. It creates (or recreates) the fileTestExamples.java
. You normally don't need to run this; theTestExamples.java
in the repository should be up to date. -
gradlew TestExamples
runs the tests inTestExamples.java
.
Alternatively, TestExamples.java
can be called as a regular JUnit test class.