0

I'm building a library written in Kotlin and packaging as a jar through Maven. In my pom I have a dependency on kotlin-reflect artifact. I'm able to compile and package successfully, but when I then try to utilize this jar within another project (i.e., a service) I get an error saying: "Kotlin reflection implementation is not found at runtime. Make sure you have kotlin-reflect.jar in the classpath". If I include the dependency in the pom of the service everything works, but I want the library jar itself to have that dependency already without consumers of the library having to add this dependency to their pom.xml.

I have tried changing the scope in the dependency from compile to runtime. I just don't understand why the kotlin-reflect wouldn't be getting packaged within the jar of the library.

<dependency>
  <groupId>org.jetbrains.kotlin</groupId>
  <artifactId>kotlin-reflect</artifactId>
  <version>1.3.50</version>
</dependency>

Error that occurs in the service:

Exception in thread "main" kotlin.jvm.KotlinReflectionNotSupportedError: Kotlin reflection implementation is not found at runtime. Make sure you have kotlin-reflect.jar in the classpath.

Thomas
  • 29
  • 2

1 Answers1

0

What you've built is a ‘thin’ jar: it includes only the classes compiled from the code in your project.  It does not include the dependencies.

(You still need to specify the dependencies in your POM, else it won't be able to compile your code.)

So your main options are:

  • Distribute your ‘thin’ jar.  If this is for your own use, you could copy the dependency jars to your target environment, and add them to your classpath in addition to the one you built.  Or for a library you provide externally, you could simply list the dependencies in the POM you publish; your users should then pick them up from there.

  • Build a ‘fat’ jar which bundles all the dependencies in addition to your code.  See this question for some info on that.

If this were for a web app, especially using Spring or similar, you could instead build a .war file, which bundles the dependencies anyway.

gidds
  • 16,558
  • 2
  • 19
  • 26