Kotlin compiler cannot find imports


#1

My main.kt file looks like this:

import speech.Hi

fun main(args: Array<String>){
   val sayHello = Hi()
   val message = sayHello.speak() 
   println(message)
}

When I run this in IntelliJ, it works just fine.

When I attempt to compile it with kotlinc and run it in the Windows command line on the other hand, it gives me an “unresolved reference” on my speech package.

What am I doing wrong?


#2

How exactly you’re running the compiler? You need to give the entire directory containing your project source code as the argument to kotlinc. If you just give it a single file as an argument, it won’t be able to import any other files.


#3

My file structure looks like this:

I’ve been running kotlinc main.kt and kotlcinc main.kt -cp speech without any luck.


#4

Does the Hi.kt file actually declare the package to be speech? Unlike Java, Kotlin is fine with classes not living in a directory that corresponds to its package.


#5

You need to run “kotlinc src”.


#6

Yes, it has a declaration for the speech package.


#7

When I run kotlinc src, I don’t get any output, however, it does compile without errors.