1

I've created a Maven project, that is using Kotlin as the main language.
The content of the POM.xml file looks as the following:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <properties>
        <kotlin.version>1.3.21</kotlin.version>
        <kotlin.compiler.incremental>true</kotlin.compiler.incremental>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <main.class>com.sweetsoft.AppKt</main.class>
    </properties>

    <groupId>com.sweetsoft</groupId>
    <artifactId>simplekotlin</artifactId>
    <version>1.0-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-maven-plugin</artifactId>
                <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/test/kotlin</sourceDir>
                                <sourceDir>${project.basedir}/src/test/java</sourceDir>
                            </sourceDirs>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <executions>
                    <!-- Replacing default-compile as it is treated specially by maven -->
                    <execution>
                        <id>default-compile</id>
                        <phase>none</phase>
                    </execution>
                    <!-- Replacing default-testCompile as it is treated specially by maven -->
                    <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>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>${main.class}</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <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>${main.class}</mainClass>
                                </manifest>
                            </archive>
                            <descriptorRefs>
                                <descriptorRef>jar-with-dependencies</descriptorRef>
                            </descriptorRefs>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        <sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
        <testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-stdlib</artifactId>
            <version>${kotlin.version}</version>
        </dependency>
    </dependencies>

</project>

The file structure looks as the following:

enter image description here

After the packaging, I carried out the JAR file and got the following error message:

/kotlin/simplekotlin/target$ java -jar simplekotlin-1.0-SNAPSHOT.jar 
Exception in thread "main" java.lang.NoClassDefFoundError: kotlin/jvm/internal/Intrinsics
        at com.sweetsoft.AppKt.main(App.kt)
Caused by: java.lang.ClassNotFoundException: kotlin.jvm.internal.Intrinsics
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
        ... 1 more

The content of the App.kt file looks as the follwing:

package com.sweetsoft

fun main(args : Array<String>) {
    println("Hello, world!")
}
softshipper
  • 32,463
  • 51
  • 192
  • 400

1 Answers1

1

The kotlin.jvm.internal.Intrinsics class is the crucial part of the Kotlin runtime, which is included in the Kotlin standard library kotlin-stdlib. Your exception means that there's a call from your compiled code to the Kotlin runtime, and the standard library is not on the runtime classpath.

To fix this, you can run the program by providing the classpath of multiple JARs via -cp:

java -cp kotlin-stdlib.jar -jar simplekotlin-1.0-SNAPSHOT.jar 

(replace kotlin-stdlib.jar with the actual path to the Kotlin standard library JAR)

See also: How to run Kotlin class from the command line?

hotkey
  • 140,743
  • 39
  • 371
  • 326
  • How to run without `kotlin-stdlib.jar`?. – softshipper Mar 14 '19 at 15:03
  • @zero_coding Yes, the POM is valid: you declared the dependency on `kotlin-stdlib` there, however, it is up to you to publish your library/application in a way that the consumers either get the information about that dependency and can thus download and add it themselves (this is usually automated) or package your library so that it includes all of the dependencies. – hotkey Mar 14 '19 at 15:29
  • To bundle the library's dependencies within its package, consider building what's called a *fat JAR* – look it up, there are Maven plugins that can build it. Once it's done, the Kotlin standard library (and other dependencies, if any) will get included into your library JAR. There are downsides of this approach, though. – hotkey Mar 14 '19 at 15:32
  • What is the downside of fat JAR? – softshipper Mar 14 '19 at 18:23