Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Programming Kotlin

You're reading from   Programming Kotlin Get to grips quickly with the best Java alternative

Arrow left icon
Product type Paperback
Published in Jan 2017
Publisher Packt
ISBN-13 9781787126367
Length 420 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
Stefan Bocutiu Stefan Bocutiu
Author Profile Icon Stefan Bocutiu
Stefan Bocutiu
Stephen Samuel Stephen Samuel
Author Profile Icon Stephen Samuel
Stephen Samuel
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Getting Started with Kotlin FREE CHAPTER 2. Kotlin Basics 3. Object-Oriented Programming in Kotlin 4. Functions in Kotlin 5. Higher Order Functions and Functional Programming 6. Properties 7. Null Safety, Reflection, and Annotations 8. Generics 9. Data Classes 10. Collections 11. Testing in Kotlin 12. Microservices with Kotlin 13. Concurrency

Mixing Kotlin and Java in a project

Using different languages within the same project is quite common; I came across projects where a mix of Java and Scala files formed the code base. Could we do the same with Kotlin? Absolutely. Let's work on the project created earlier, Kotlin with Gradle. You should see the following directory structure in your IntelliJ (the standard template for a Java/Kotlin project):

Mixing Kotlin and Java in a project

Project layout

You can place Java code within the java folder. Add a new package to the java folder with the same name as the one present in the kotlin folder: com.programming.kotlin.chapter01. Create a New | Java class named CarManufacturer.java and use this code for the purpose of the exercise:

    public class CarManufacturer { 
      private final String name; 
      public CarManufacturer(String name) { 
        this.name = name; 
      } 
      public String getName() { 
        return name; 
      } 
    } 

What if you want to add a Java class under the kotlin subfolder? Let's create a Student class similar to the previous one and provide a field name for simplicity:

    public class Student { 
      private final String name; 
      public Student(String name) { 
        this.name = name; 
      } 
      public String getName() { 
        return name; 
      } 
    } 

In the main function, let's instantiate our classes:

    fun main(args: Array<String>) { 
      println("Hellow World!") 
      val student = Student("Alexandra Miller") 
      println("Sudent name:${student.name}") 
      val carManufacturer = CarManufacturer("Mercedes") 
      println("Car manufacturer:${carManufacturer.name}") 
    } 

While the code compiles just fine, trying to run it will throw a runtime exception, saying that it can't find the Student class. We need to let the Java compiler look for code under the src/main/kotlin folder. In your gradle.build, add the following instruction:

    sourceSets { 
      main.java.srcDirs += 'src/main/kotlin' 
    } 

Now we can compile and run the program:

$gradle jar
$ java -jar build/libs/chapter01-1.0-SNAPSHOT.jar

As your Kotlin code gets bigger, compilation will slow down since it will have to go and recompile each file. There is a way to speed it up, though: by only compiling files changed between builds. The easiest way to enable this is to create a file called gradle.properties alongside build.gradle and add kotlin.incremental=true to it. While the first build will not be incremental, the following ones will be, and you should see your compilation time cut down quite a bit.

Maven is still, probably, the most used build system on the JVM. So let's see how we can achieve our goal of mixing Kotlin and Java code in Maven. Starting with IntelliJ, choose New | Project, pick Maven as the option, and look for kotlin-archetype-jvm from the list of archetypes. We already covered this, so it should be a lot easier the second time around. We now have a project.

From the project tree, you will notice that there is no java folder source code created. Go ahead and create src/main/java, followed by the namespace folder com.programming.kotlin (this will be a subfolder of the java one). You will notice that right-clicking on the java folder won't give you the option to create a package. The project is not yet configured to include Java code. But first, what makes Maven handle Kotlin code? If you open the pom.xml file and go to the plugins section, you will notice the kotlin plugin:

    <plugin> 
      <groupId>org.jetbrains.kotlin</groupId> 
      <artifactId>kotlin-maven-plugin</artifactId> 
      <version>${kotlin.version}</version> 
      <executions> 
        <execution> 
          <id>compile</id> 
          <phase>compile</phase> 
          <goals> 
            <goal>compile</goal> 
          </goals> 
        </execution> 
        <execution> 
          <id>test-compile</id> 
          <phase>test-compile</phase> 
          <goals> 
            <goal>test-compile</goal> 
          </goals> 
        </execution> 
      </executions> 
    </plugin> 

To add Java code to the mix, we need to set a new plugin that will be able to compile good old Java:

    <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-compiler-plugin</artifactId> 
      <version>3.5.1</version> 
      <executions> 
        <execution> 
          <id>default-compile</id> 
          <phase>none</phase> 
        </execution> 
        <execution> 
          <id>default-testCompile</id> 
          <phase>none</phase> 
        </execution> 
        <execution> 
          <id>java-compile</id> 
          <phase>compile</phase> 
          <goals> 
            <goal>compile</goal> 
          </goals> 
        </execution> 
        <execution> 
          <id>java-test-compile</id> 
          <phase>test-compile</phase> 
          <goals> 
            <goal>testCompile</goal> 
          </goals> 
        </execution> 
      </executions> 
    </plugin> 

The Kotlin compiler has to run before the Java compiler to get it all working, so we will need to amend the Kotlin plugin to do just that:

    <plugin> 
      <artifactId>kotlin-maven-plugin</artifactId> 
      <groupId>org.jetbrains.kotlin</groupId> 
      <version>${kotlin.version}</version> 
      <executions> 
        <execution> 
          <id>compile</id> 
          <goals> 
            <goal>compile</goal> 
          </goals> 
          <configuration> 
            <sourceDirs> 
              <sourceDir>${project.basedir}/src/main/kotlin</sourceDir> 
              <sourceDir>${project.basedir}/src/main/java</sourceDir> 
            </sourceDirs> 
          </configuration> 
        </execution> 
        <execution> 
          <id>test-compile</id> 
          <goals> 
            <goal>test-compile</goal> 
          </goals> 
          <configuration> 
            <sourceDirs> 
              <sourceDir>${project.basedir}/src/main/kotlin</sourceDir> 
              <sourceDir>${project.basedir}/src/main/java</sourceDir> 
            </sourceDirs> 
          </configuration> 
        </execution> 
      </executions> 
    </plugin> 

To be able to produce the executable JAR for the code we are about to write, we need yet another Maven plugin:

    <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-jar-plugin</artifactId> 
      <version>3.0.2</version> 
      <configuration> 
        <archive> 
          <manifest> 
            <addClasspath>true</addClasspath> 
            <mainClass>com.programming.kotlin.HelloKt</mainClass> 
          </manifest> 
        </archive> 
      </configuration> 
    </plugin> 

The preceding code will give you a JAR containing just your code; if you want to run it then you need the extra dependencies to the classpath:

    <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-assembly-plugin</artifactId> 
      <version>2.6</version> 
      <executions> 
        <execution> 
          <id>make-assembly</id> 
          <phase>package</phase> 
          <goals> <goal>single</goal> </goals> 
          <configuration> 
            <archive> 
              <manifest> 
                <mainClass>com.programming.kotlin.HelloKt</mainClass> 
              </manifest> 
            </archive> 
            <descriptorRefs> 
              <descriptorRef>jar-with-dependencies</descriptorRef> 
            </descriptorRefs> 
          </configuration> 
        </execution> 
      </executions> 
    </plugin> 

Now we are in a position to add the classes from the previous example (the CarManufacturer and Student classes) and change the main class to contain the following:

    val student = Student("Jenny Wood") 
    println("Student:${student.name}") 
    val carManufacturer = CarManufacturer("Honda") 
    println("Car manufacture:${carManufacturer.name}") 

This is not ready yet. While compiling will go well, trying to execute the JAR will yield an error at runtime about the Student class not being found. The Java compiler needs to know about the Java code sitting under the kotlin folder. For that, we bring in another plugin:

    <plugin> 
      <groupId>org.codehaus.mojo</groupId> 
      <artifactId>build-helper-maven-plugin</artifactId> 
      <executions> 
        <execution> 
          <phase>generate-sources</phase> 
          <goals><goal>add-source</goal></goals> 
          <configuration> 
            <sources> 
              <source>${project.basedir}/src/main/kotlin</source> 
            </sources> 
          </configuration> 
        </execution> 
      </executions> 
    </plugin> 

Finally, we are in a position to compile and run the code. Executing the commands in a terminal will end up printing three lines in the output:

$ mvn package
$ java -jar target/chapter01-maven-mix-1.0-SNAPSHOT-jar-with-dependencies.jar
You have been reading a chapter from
Programming Kotlin
Published in: Jan 2017
Publisher: Packt
ISBN-13: 9781787126367
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image