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

后端 未结 12 1990
死守一世寂寞
死守一世寂寞 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 15:02

    In my case I also needed to check that object which is not found as a member of package is compiled successfully.

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

    I had faced similar issue where IntelliJ showed error on importing one file from the same project.

    What did not resolve the issue in my case:

    1. adding _root_ in import statement
    2. sbt clean
    3. restarting machine

    What actually resolved the issue:

    1. main menu => select File => click on Invalidate Caches / Restart => pop-up dailog => click on invalidate the caches and restart.

    I was using IDEA (2019.2.2 Ultimate Edition) on macOs mojave 10.14.6

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

    I had a similar situation, which was failing in both IntelliJ and maven on the command line. I went to apply the suggested temp fix (adding _root_) but intellij was glitching so bad that wasn't even possible.

    Eventually I noticed that I had mis-created a package so that it repeated the whole path of the package. That meant that the directory my class was in had a subfolder called "com", and the start of my file looked like:

    package com.mycompany.mydept.myproject.myfunctionality.sub1
    
    import com.holdenkarau.spark.testing.DataFrameSuiteBase 
    

    where I had another package called com.mycompany.mydept.myproject.myfunctionality.sub1.com.mycompany.mydept.myproject.myfunctionality.sub2

    And the compiler was looking for "holdenkarau" under com.mycompany.mydept.myproject.myfunctionality.com and failing.

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

    In my case, In Intellij, Just renaming the package file to something else >> see if it updates the import statements >> run the code >> then renaming back to the original name worked.

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

    imports can be relative. Is that the only import you have? be careful with other imports like

    import com.me

    ultimately, this should fix it, then you can try to find more about it:

    import _root_.com.me.project.database.Database

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

    If you are using gradle as your build tool, then ensure that jar task is not disabled.

    I had multiple modules in my project, where one module was dependent on a few other modules. However, I had disabled jar task in build.gradle:

    jar { enabled = false }

    That caused it to fail to resolve classes in the dependent modules and fail with the above error.

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