Scala import not working - object is not a member of package, sbt preppends current package namespace in imports

后端 未结 12 1988
死守一世寂寞
死守一世寂寞 2020-12-14 14:28

I have an issue when trying to import in scala. The object Database exists under com.me.project.database but when I try to import it:



        
相关标签:
12条回答
  • 2020-12-14 14:46

    I had a similar problem but none of the solutions here worked for me. What did work however was a simple restart of my machine.

    Perhaps it was something with my Intellij but after a quick restart, everything seems to be working fine.

    0 讨论(0)
  • 2020-12-14 14:48

    I had this issue while using Intellij and the built-in sbt shell (precisely, I was trying to run the command console, which invokes a compiler check of the code).

    In my case, after trying the other suggested solutions on this thread, I found that I could restart the sbt shell and it would go away. There's a button on the left-hand side of a looped green arrow and a small grey square which does this in one click (obviously, this is subject to Jet Brains not changing the design of the IDE!!!).

    I hope this helps some people get past this issue quickly.

    0 讨论(0)
  • 2020-12-14 14:49

    Java -> Scala conversion without cleaning

    Don't forget to clean if you convert some file in a project from Java to Scala. I had a continuous integration build running where I couldn't get things to work, even though the build was working locally, after I had converted a Java class into a Scala object. Solution: add 'clean' to the build procedure on the CI server. The name of the generated .class file in Scala is slightly different than for a Java class, I believe, so this is very likely what was causing the issue.

    0 讨论(0)
  • 2020-12-14 14:50

    In my case I had to run sbt clean.

    0 讨论(0)
  • 2020-12-14 14:53

    I will share my story, just in case it may help someone.

    Scenario: intellij compilation succeeds, but gradle build fails on import com.foo.Bar, where Bar is a scala class.

    TLDR reason: Bar was located under src/main/java/... as opposed to src/main/scala/...

    Actual reason: Bar was not being compiled by compileScala gradle task (from gradle scala plugin) because it looks for scala sources only under src/<sourceSet>/scala.

    From docs.gradle.org:

    All the Scala source directories can contain Scala and Java code. The Java source directories may only contain Java source code.

    Hope this helps

    0 讨论(0)
  • 2020-12-14 15:01

    I realize this question already has an accepted answer, but since I experienced the same problem but with a different cause I figured I'd add an answer.

    I had a bunch of interdependent projects which suddenly needed a root import in order to compile. It turned out that I had duplicated the package declaration in a single file. This caused some kind of chain reaction and made it very hard to find the source of the problem.

    In summary I had

    package foo.bar
    package foo.bar
    

    on the top of the file instead of just

    package foo.bar
    

    Hope this saves someone some really tedious error hunting.

    0 讨论(0)
提交回复
热议问题