How to compile and run a kotlin file which has dependencies on a jar from CLI?

752 views Asked by At

I have a file called A.kt which has dependencies on B.jar. How to compile and run the kotlin file on CLI?

I have tried:

kotlinc A.kt -cp B.jar

but this did not work.

The error I get is:

A.kt:11:24: error: unresolved reference: B
        val b = B(
1

There are 1 answers

2
gidds On

Running kotlinc -help shows:

Usage: kotlinc-jvm <options> <source files>

That puts the options before the source files.  Which is pretty standard: most commands process their arguments in order, so I'd expect the command in the question to compile A.kt before setting up the classpath to include B.jar.

So you should try:

kotlinc -cp B.jar A.kt

Other potential causes include B.jar not being present in the current directory (or not being given along with the necessary path info), and it not containing the relevant .class files (in directories corresponding to their packages).