1
votes

I'm trying to create a proof of concept for Kotlin calling Scala code.

Here's what the project looks like at the moment:

kotlin-src/
  hello.kt
scala-src/
  Hello.scala

Then to compile both languages:

kotlinc kotlin-src/*.kt
scalac scala-src/*.scala

Which produces the following files in the root directory:

META-INF/
  main.kotlin_module
HelloKt.class
HelloScala.class

That I attempt to turn into a JAR with:

jar cvfM run.jar *.class META-INF

However, it won't run (and I assume it's because I'm not specifying a main class).

$ java -jar run.jar
Error: Invalid or corrupt jarfile run.jar

So, I've created the following manifest:

Main-Class: HelloKt

Before compiling the jar, this file gets copied into the META-INF directory and the structure of the resulting jar is as follows:

HelloKt.class
HelloScala.class
META-INF/
META-INF/main.kotlin_module
META-INF/MANIFEST.MF

The new JAR will execute, but always fails with a runtime exception.

$ java -jar run.jar
Exception in thread "main" java.lang.NoClassDefFoundError: kotlin/jvm/internal/Intrinsics
    at HelloKt.main(hello.kt)
Caused by: java.lang.ClassNotFoundException: kotlin.jvm.internal.Intrinsics
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 1 more

What are the remaining steps needed to get the JAR compiled and running the main method in the Kotlin class?

Are there runtime classes that need to be bundled with the Scala/Kotlin code to make this work?

1
The error suggests that you haven't got the kotlin stdlib in your classpath - s1m0nw1

1 Answers

2
votes

I haven't tested it, but it should work:

  1. Compile your code as you have
  2. Download kotlin-stdlib jar from Maven Central
  3. Verify that you used the same version of the stdlib while compiling
  4. Add directory with the stdlib jar to the classpath parameter of the java command when running the jar