You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

IDEA Community 2021.2.3运行Scala工作表时无法识别自定义包问题排查求助

Troubleshooting "not found: value PUBL" in Scala Worksheet (IDEA Community)

First, let's recap your setup to align on the context:

  • Environment: JDK 1.8, Scala 2.12, IDEA Community 2021.2.3
  • You created this package and object:
package PUBL
import java.text.SimpleDateFormat
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import java.util.{Calendar, Date, GregorianCalendar}
object DATE {
  def strDateAdd(strDateInpu: String, intNumbDay: Int): String = {
    val dateFormat = new SimpleDateFormat("yyyyMMdd")
    val datDateInpu: Date = dateFormat.parse(strDateInpu)
    val calendar = new GregorianCalendar
    calendar.setTime(datDateInpu)
    calendar.add(Calendar.DATE, intNumbDay)
    val datDateOutp: Date = calendar.getTime()
    dateFormat.format(datDateOutp)
  }
}
  • When running this code in a Scala Worksheet:
import PUBL.DATE.strDateAdd
println(strDateAdd("20210101",10))

IDEA recognizes the import syntax, but you get not found: value PUBL when evaluating the worksheet.

Let's walk through the key checks to fix this issue:

  • Confirm your source root configuration
    Make sure the .scala file containing the PUBL package lives in a directory marked as a Sources Root in IDEA. By default, this is src/main/scala or src/test/scala. If your file is in a regular unmarked folder, IDEA won't compile it as part of your project. To fix this:

    1. Right-click the folder holding your PUBL package
    2. Select Mark Directory as → Sources Root
  • Check module dependencies and Scala SDK consistency

    • For multi-module projects, ensure the module with your Scala Worksheet depends on the module where PUBL is defined. Set this up via File → Project Structure → Modules → Dependencies.
    • Verify both your PUBL code and the worksheet use the same Scala SDK version (2.12.x). Check this in File → Project Structure → Modules for each relevant module. Mismatched SDK versions can cause unexpected visibility gaps.
  • Force a full project rebuild
    IDEA doesn't always auto-compile changes immediately. Try rebuilding your project completely: Build → Rebuild Project. This ensures your DATE object is compiled and available for the worksheet to reference. Also, check the Build tool window for any compilation errors in your PUBL code—if it fails to compile, the worksheet won't be able to find it.

  • Adjust Scala Worksheet scope settings
    Right-click your worksheet file and select Open Module Settings. Check the Scope setting:

    • If your PUBL code is in the Main source root, ensure the worksheet's scope is set to Production (or that the Test scope includes Main sources). A Test-scoped worksheet might not access non-test code by default.
  • Clear IDEA caches and restart
    Sometimes IDEA's cached state causes false "not found" errors. Try invalidating caches: File → Invalidate Caches... → select Invalidate and Restart. This often resolves odd visibility issues that don't make sense otherwise.

  • Double-check package name case sensitivity
    Scala is case-sensitive with package names. Confirm your import import PUBL.DATE.strDateAdd uses the exact same case as your package declaration package PUBL (your code looks correct, but it's worth a quick typo check).

内容的提问来源于stack exchange,提问作者davidzxc574

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 06:53:05