Kotlin compiler cannot find imports

My main.kt file looks like this:

import speech.Hi

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

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?

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.

1 Like

My file structure looks like this:

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

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.

1 Like

You need to run “kotlinc src”.

Yes, it has a declaration for the speech package.

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

i also get same error
i already try kotlinc src but still have same error

Did you find the solution for this.I am facing the same problem.
Kindly give the command line code to compile

1 Like

Just struggled with the same problem and my solution was to run that way:

  • Download kotlin’s latest release from here: Releases · JetBrains/kotlin · GitHub - At the time of this post it was “v1.5.20” (the zip file to download is located at the bottom of the page)
  • Unzip “kotlin-compiler-1.5.20.zip” it in a place of your choice. Locate the bin folder and copy its path to your environment path (on Windows)
  • It should look something like this: your_download_location\kotlinc\bin
  • Open your system settings and edit the system environment variable called “PATH” by adding a new entry to it using the located path above - System environment path can be edited on Windows over: System Settings > System and Security > System > Advanced system setting > Environment variables…
  • Compile the program using the kotlin compiler command bellow by opening a terminal in the root of this project:
kotlinc src your_project_location/your_project_name/src/main/kotlin/your_main_class.kt -include-runtime -d your_outputname.jar


kotlinc src c:/repo/myproject/src/main/kotlin/Main.kt -include-runtime -d main.jar

Run Result without args

java -jar main.jar 

Run result passing arguments to it

java -jar main.jar arg1 arg2