How do I add dependencies (kotlin)

How do I add dependencies, e.g., jar files or gradle dependencies so I have access to libraries like OpenCSV or Jackson (JSON serialization)?

I’m supposed to click the environment button on the left-hand side but when I do nothing happens. Is that Kotlin specific?

I sort of figured out one approach which is to upload the jar files as files and then include them using something like:

@file:DependsOn("/data/notebook_files/opencsv-5.7.1.jar")

but there’s something weird going on with the class loaders because if there’s dependencies between them they they don’t see each other. For instance this works;

import org.apache.commons.lang3.ObjectUtils
ObjectUtils()

but then this fails and says that the opencsv package can’t see the lang3 packages.

@file:DependsOn("/data/notebook_files/commons-lang3-3.12.0.jar")

@file:DependsOn("/data/notebook_files/opencsv-5.7.1.jar")

import com.opencsv.*

import java.io.File

val file = File ("/data/notebook_files/salary.csv")

val csvReader = CSVReaderBuilder(file.reader ()).withSkipLines(2).build()

val allData = csvReader.readAll()


with

org/apache/commons/lang3/ObjectUtils
java.lang.NoClassDefFoundError: org/apache/commons/lang3/ObjectUtils
	at com.opencsv.CSVParserBuilder.withErrorLocale(CSVParserBuilder.java:211)
	at com.opencsv.CSVReaderBaseBuilder.getOrCreateCsvParser(CSVReaderBaseBuilder.java:97)
	at com.opencsv.CSVReaderBuilder.build(CSVReaderBuilder.java:95)
	at Line_50.<init>(Line_50.jupyter-kts:9)

Hi! Each dependency exists in its separate classloader, and that’s the reason dependencies don’t see each other.

The best and easiest way to achieve what you want is to use maven coordinates for your libraries:
@file:DependsOn(“group:artifact:version”)

Corresponding coordinates could be easily found on Maven Search portal:

Also, refer to Kotlin kernel Readme: