Install Kotlin and use the command line

Install Kotlin and use the command line

The purpose of the first lab is to install Kotlin on your computer, to learn how to use a command line, and to run Kotlin from the command line.

The explanations below are for Windows. If you use Mac OSX or Linux, please check the remarks at the end of this page.

Start a command line

From the Windows menu, find the terminal program (under "Accessories"). Alternatively, press Windows+R and type cmd to start the terminal program.

Install Java if necessary

Type java -version in your terminal. If your output indicates that you have Java 1.6 or higher installed, then you are done. Otherwise, you need to install Java. Click on Windows Online at www.java.com.

Restart your terminal program after the Java installation and check that Java is now on your computer.

Install Kotlin

Please use exactly version 1.2.21 of the compiler, so that all CS109 students are running the exact same environment.

Extract the zip file to C:\Program Files. You need to add the kotlinc\bin directory to your command line path - the TAs will show you how.

Restart your terminal program, and check that you can start Kotlin by saying kotlinc.

Try some simple interactive Kotlin statements, such as

$kotlinc Welcome to Kotlin version 1.2.21 (JRE 1.8.0_151-8u151-b12-0ubuntu0.16.04.2-b12) Type :help for help, :quit for quit >>> 7 * 3 21 >>> 7 * Math.PI 21.991148575128552  Install additional files Download cs109-additions.zip and extract the files in exactly the same place where you extracted the Kotlin archive. This package contains: • A few commandline commands called ktc, kts, kt, and kttest, that will make your life easier by managing the dependencies on the libraries we use, and by organizing all class files in a subdirectory of your source directory. • Two modules (cs109 and cs109ui) that make it possible to write programs with a simple graphical user interface without learning about the Swing library or worrying about event-based programming. • Two libraries for unit test from junit.org. • A tool for converting Java classes to the Android DEX format, for our Android Mini-App framework. This makes it possible to write simple Android apps without having to install the Android SDK. A few checks Here are few checks you can perform to see if everything works: • Say ktc on the command line. The interactive Kotlin mode should start. • Download uitest1.kt. Run it from the command line: $ ktc uitest1.kt
$kt Uitest1Kt  A new window should appear. • Try importing junit: $ ktc
Welcome to Kotlin version 1.2.21 (JRE 1.8.0_151-8u151-b12-0ubuntu0.16.04.2-b12)
Type :help for help, :quit for quit
>>> import org.junit.Test


For a more complete test of junit, download the example test suite test1.kt, compile it, and run it:

$ktc test1.kt$ kttest AdditionTest
JUnit version 4.12
.
Time: 0.003

OK (1 test)


The output should show that everything went alright and that all tests were passed.

You can use any editor you like for writing Kotlin programs. Perhaps you are already using VIM or Emacs—then keep using them.

Otherwise we recommend that you use Notepad++. Download and install:

Finally, download the file kotlin.xml and save it to your desktop. Start Notepad++, and select "User defined language" from the "Language" menu. Press the "Import" button, and select the kotlin.xml file. After the message "Import successful", terminate Notepad++.

When you start Notepad++ again, you should see "Kotlin" in the "Language" menu, and the language should automatically switch to Kotlin when you open a file with extension kt or kts.

Write a Kotlin script

Open your editor, and write the following small program:

println("Your arguments are:")

for (i in 0 until args.size) {
println("$i:${args[i]}")
}

Save it as a file named args.kts.

Try running this program from the command line, with arguments, such as:

\$ kts args.kts Hello World!


Try a larger program

Finally, you may want to try running a larger program, such as mastermind.kts.

Mac OSX and Linux users

There are several ways to install Kotlin on Unix systems (including OSX), but I strongly recommend that you simply use the same zip file kotlin-compiler-1.2.21.zip linked above, to make sure that you have an environment compatible with the class environment (I have tested everything both on Linux and OSX).

To summarize:

• Install Java (using your package manager, or directly from Oracle);
• Unzip kotlin-compiler-1.2.21.zip in a suitable place, for instance in your home directory, or in ~/Library on OSX;
• Unzip cs109-additions.zip in the same place;
• Add the kotlinc/bin directory to your path;
• Try the tests above from the Unix command line.

You can use any editor you like (but don't use an integrated development environment like Eclipse or IntelliJ in this course).

 Install Kotlin and use the command line