Scala 导入不起作用 - 对象 <name> 不是包的成员,sbt 在导入中预先添加当前包命名空间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21701452/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Scala import not working - object <name> is not a member of package, sbt preppends current package namespace in imports
提问by Neilos
I have an issue when trying to import in scala. The object Databaseexists under com.me.project.databasebut when I try to import it:
尝试在 Scala 中导入时遇到问题。该对象Database存在于com.me.project.database但当我尝试导入它时:
import com.me.project.database.Database
I get the error:
我收到错误:
object Database is not a member of package com.me.project.controllers.com.me.project.database
Any ideas what the problem is?
任何想法是什么问题?
Edit:
编辑:
It is worth mentioning that the import is in the file Application.scala under the package com.me.project.controllers, I can't figure out why it would append the import to the current package though, weird...
值得一提的是,导入在包下的 Application.scala 文件中com.me.project.controllers,我不明白为什么它会将导入附加到当前包中,奇怪...
Edit 2:
编辑2:
So using:
所以使用:
import _root_.com.me.project.database.Database
Does work as mentioned below. But should it work without the _root_? The comments so far seem to indicate that it should.
是否按照下面提到的方式工作。但是它应该在没有_root_? 迄今为止的评论似乎表明它应该这样做。
Answer:
回答:
So it turns out that I just needed to clean the project for the import to work properly, using both:
所以事实证明,我只需要清理项目以使导入正常工作,同时使用:
import _root_.com.me.project.database.Database
import com.me.project.database.Database
are valid solutions. Eclipse had just gotten confused.
是有效的解决方案。Eclipse 刚刚被搞糊涂了。
采纳答案by fracca
imports can be relative. Is that the only import you have? be careful with other imports like
进口可以是相对的。那是你唯一的进口吗?小心其他进口,如
import com.me
import com.me
ultimately, this should fix it, then you can try to find more about it:
最终,这应该解决它,然后你可以尝试找到更多关于它的信息:
import _root_.com.me.project.database.Database
import _root_.com.me.project.database.Database
回答by Bunyk
In my case I also needed to check that object which is not found as a member of package is compiled successfully.
在我的情况下,我还需要检查未作为包成员找到的对象是否已成功编译。
回答by r90t
In my case I had to run sbt clean.
就我而言,我必须运行sbt clean.
回答by Rikard
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.
希望这可以为某人节省一些非常乏味的错误搜索。
回答by bbarker
Java -> Scala conversion without cleaning
Java -> Scala 转换无需清理
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.
如果您将项目中的某些文件从 Java 转换为 Scala,请不要忘记清理。在我将 Java 类转换为 Scala 对象之后,我有一个持续集成构建运行,但我无法让事情正常工作,即使构建在本地工作。解决方案:在 CI 服务器上的构建过程中添加“clean”。我相信,Scala 中生成的 .class 文件的名称与 Java 类的名称略有不同,因此这很可能是导致问题的原因。
回答by panther
If you are using gradleas your build tool, then ensure that jartask is not disabled.
如果您将其gradle用作构建工具,请确保jar未禁用该任务。
I had multiple modules in my project, where one module was dependent on a few other modules. However, I had disabled jartask in build.gradle:
我的项目中有多个模块,其中一个模块依赖于其他几个模块。但是,我在以下位置禁用了jar任务build.gradle:
jar {
enabled = false
}
jar {
enabled = false
}
That caused it to fail to resolve classes in the dependent modules and fail with the above error.
这导致它无法解析依赖模块中的类并因上述错误而失败。
回答by Dima Ogurtsov
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 Baris a scala class.
场景:intellij 编译成功,但 gradle build 失败import com.foo.Bar,其中Bar是 Scala 类。
TLDR reason:Barwas located under src/main/java/...as opposed to src/main/scala/...
TLDR理由:Bar位于下src/main/java/...,而不是src/main/scala/...
Actual reason:Barwas not being compiled by compileScalagradle task (from gradle scala plugin) because it looks for scala sources only under src/<sourceSet>/scala.
实际原因:Bar没有被compileScalagradle 任务编译(来自 gradle scala 插件),因为它只在src/<sourceSet>/scala.
From docs.gradle.org:
All the Scala source directories can contain Scala and Java code. The Java source directories may onlycontain Java source code.
所有 Scala 源目录都可以包含 Scala 和 Java 代码。Java 源目录可能只包含 Java 源代码。
Hope this helps
希望这可以帮助
回答by AlphaCR
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.
也许这是我的 Intellij 的问题,但在快速重启后,一切似乎都运行良好。
回答by MrSpaceman
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).
我在使用 Intellij 和内置sbt shell时遇到了这个问题(准确地说,我试图运行命令控制台,它调用代码的编译器检查)。
In my case, after trying the other suggested solutions on this thread, I found that I could restart the sbt shelland 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!!!).
就我而言,在此线程上尝试了其他建议的解决方案后,我发现我可以重新启动sbt shell并且它会消失。环形绿色箭头左侧有一个按钮和一个灰色小方块,单击即可完成此操作(显然,这取决于 Jet Brains 不会更改 IDE 的设计!!!)。
I hope this helps some people get past this issue quickly.
我希望这可以帮助一些人快速解决这个问题。
回答by Adair
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.
我遇到了类似的情况,在命令行中的 IntelliJ 和 maven 中都失败了。我去应用建议的临时修复(添加_root_),但 intellij 出现故障,甚至不可能。
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:
最终我注意到我错误地创建了一个包,因此它重复了包的整个路径。这意味着我的班级所在的目录有一个名为“com”的子文件夹,我的文件的开头看起来像:
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
我有另一个名为 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.
并且编译器正在 com.mycompany.mydept.myproject.myfunctionality.com 下寻找“holdenkarau”但失败了。

