2026-09-11 · Q&A guide

Resolve Scala compile error: object not found in package

Learn why Scala cannot locate an object in a package and how to fix the import by aligning package declarations with the directory layout.

Package declarations must match the folder hierarchy

Scala uses the package statement at the top of a source file to determine the fully‑qualified name of the class or object. If the declared package does not reflect the actual directory structure, the compiler will not be able to resolve the symbol, resulting in an error like "object scalacommon is not a member of package com.kwoolytech".

Correct the package path for Syslog

Place the file under a directory that mirrors its package name and declare the package accordingly. For example, if the object lives in `src/main/scala/com/kwoolytech/scalacommon/Syslog.scala`, the file should start with:

package com.kwoolytech.scalacommon

object Syslog {
  def emergency(s: String) = println(s"[Emergency] $s")
  def alert(s: String)      = println(s"[Alert] $s")
  def critical(s: String)   = println(s"[Critical] $s")
  def error(s: String)      = println(s"[Error] $s")
  def warning(s: String)    = println(s"[Warning] $s")
  def notice(s: String)     = println(s"[Notice] $s")
  def debug(s: String)      = println(s"[Debug] $s")
  def info(s: String)       = println(s"[Info] $s")
}

Align the Dice class with its own package

The Dice source file should also be placed under a directory that reflects its package declaration. For a package `com.kwoolytech.kwoolybot`, the path would be `src/main/scala/com/kwoolytech/kwoolybot/Dice.scala` and the file should begin with:

package com.kwoolytech.kwoolybot

import com.kwoolytech.scalacommon.Syslog

class Dice(command: List[String], callback: List[String] => Unit) extends Bot {
  override def run(): Unit = {
    command.headOption match {
      case Some("roll") => roll(command.tail, callback)
      case _ => Syslog.debug(s"${getClass.getName} Invalid command.")
    }
  }
}

Check your build tool's source roots

When using sbt, Maven, or Gradle, ensure that the source root (`src/main/scala` by default) is correctly configured. Adding an extra source root like `src/scalacommon` without updating the build definition will keep the compiler from seeing the `Syslog` object.

Recompile and verify the import works

After fixing the package statements and directory layout, run a clean build (`sbt clean compile` or `mvn clean compile`). The compiler should now resolve `com.kwoolytech.scalacommon.Syslog` without errors. If the problem persists, double‑check that there are no duplicate package declarations in other files.

Takeaway: Package declarations and folder structure must be in sync for Scala imports to succeed.

People also ask

Why does Scala complain about a missing package even though the file exists?

The compiler only searches directories that match the declared package path; a mismatch between the package statement and the file location makes the symbol invisible.

Can I keep source files in arbitrary folders?

Yes, but you must add those folders as source roots in your build configuration and ensure the package statements still reflect the logical hierarchy.

Inspired by a public discussion on Stack Overflow. This article is an original explanation for learners.

← All posts