IDEA Community 2021.2.3运行Scala工作表时无法识别自定义包问题排查求助
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.scalafile containing thePUBLpackage lives in a directory marked as a Sources Root in IDEA. By default, this issrc/main/scalaorsrc/test/scala. If your file is in a regular unmarked folder, IDEA won't compile it as part of your project. To fix this:- Right-click the folder holding your
PUBLpackage - Select
Mark Directory as→Sources Root
- Right-click the folder holding your
Check module dependencies and Scala SDK consistency
- For multi-module projects, ensure the module with your Scala Worksheet depends on the module where
PUBLis defined. Set this up viaFile → Project Structure → Modules → Dependencies. - Verify both your
PUBLcode and the worksheet use the same Scala SDK version (2.12.x). Check this inFile → Project Structure → Modulesfor each relevant module. Mismatched SDK versions can cause unexpected visibility gaps.
- For multi-module projects, ensure the module with your Scala Worksheet depends on the module where
Force a full project rebuild
IDEA doesn't always auto-compile changes immediately. Try rebuilding your project completely:Build → Rebuild Project. This ensures yourDATEobject is compiled and available for the worksheet to reference. Also, check the Build tool window for any compilation errors in yourPUBLcode—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 selectOpen Module Settings. Check the Scope setting:- If your
PUBLcode is in theMainsource root, ensure the worksheet's scope is set toProduction(or that theTestscope includes Main sources). ATest-scoped worksheet might not access non-test code by default.
- If your
Clear IDEA caches and restart
Sometimes IDEA's cached state causes false "not found" errors. Try invalidating caches:File → Invalidate Caches...→ selectInvalidate 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 importimport PUBL.DATE.strDateAdduses the exact same case as your package declarationpackage PUBL(your code looks correct, but it's worth a quick typo check).
内容的提问来源于stack exchange,提问作者davidzxc574

