56

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:

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...

Edit 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.

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.

Neilos
  • 2,696
  • 4
  • 25
  • 51
  • Could you provide a minimal example showing this compile error? – Christian Feb 11 '14 at 14:00
  • What line is the error reported from - the point of use, or the import? If from the import, is it possible that Database is not in your classpath? Then, scalac wouldn't be able to guess whether it's an absolute or relative package path, and might guess wrong, in generating the error message. – Ed Staub Feb 11 '14 at 18:27
  • It is from the import, but the Database class is in my project, it is not another project or external lib, so I shouldn't have any classpath issues (I'm coming at this from a Java perspective, I'm 2 days new to Scala). – Neilos Feb 11 '14 at 20:16
  • Man, 5 years late I've been helped by this post, having the precise same issue. I'd just like to point out that the title is a bit... bad, man. I can't find this with the average google search - took me multiple tries and, like, half an hour to get here. This question is very good and helps to clarify a very random-like issue - I would keep trying to get it to work, instead of just restart the damn SBT session. Give this more visibiity, please - improve the title to something like "Import preppending current package namespace in imports", please.. – Lucas Lima Nov 19 '19 at 16:31
  • 1
    @LucasLima this question has 39k views so some people find it, but i take your point ;) changed – Neilos Nov 20 '19 at 22:32
  • Oh, and, If I at all sounded harsh, please, forgive me, not my intention at all. The question is a life saver, I just wanted it to be faster for others to find this, great question. – Lucas Lima Nov 21 '19 at 14:22
  • @LucasLima no offence taken at all, never even entered my thoughts. Glad to have helped :) – Neilos Nov 21 '19 at 19:52

12 Answers12

27

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

THIS USER NEEDS HELP
  • 3,136
  • 4
  • 30
  • 55
fracca
  • 2,417
  • 1
  • 23
  • 22
  • 1
    @Neilos, see important edit - `_root_` is surrounded by underscores. – Ed Staub Feb 11 '14 at 18:42
  • Thanks both. I had figured out the underscores by doing a bit of googling on scala relative imports. It sitll seems very peculiar and I don't fully understand why the `_root_` tag is required, I don't much want to litter my code with `_root_` before every import. Nevertheless this does work. – Neilos Feb 11 '14 at 20:09
  • avoid importing packages or importing everyhing in a package like com.me._ – fracca Feb 12 '14 at 09:00
20

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

Bunyk
  • 7,635
  • 8
  • 47
  • 79
17

In my case I had to run sbt clean.

r90t
  • 366
  • 3
  • 15
  • Exact same thing here! I had both Java and Scala code and a package name in both. After refactoring the compiler would complain with this error for no apparent reason =/ `sbt clean && sbt compile` worked for me! – Matt Klein Jun 18 '17 at 19:38
  • 2
    you can chain sbt tasks by like running `sbt clean compile` instead of `sbt clean && sbt compile` – r90t Jun 22 '17 at 08:03
16

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.

Rikard
  • 692
  • 4
  • 14
  • Just an FYI, if I do the same in Eclipse then it gives a compiler error, I recommend using an IDE and if you are then I recommend checking the error reporting (in Eclipse Luna it is Window>Preferences>Java>Compiler>Errors/Warnings) – Neilos Jan 14 '15 at 00:39
  • Thanks, I'm using Scala IDE 4.0.0 with 2.10 compiler settings and JVM target set to 1.7. I know it's illegal in java, but in Scala it should be valid I believe. [Tried adding a working ideone.com link, but ideone has problems with package declarations it seems.] It does work in my Scala IDE. Multiple package declarations should work in Scala. See http://stackoverflow.com/questions/3541053/multiple-packages-definition – Rikard Jan 14 '15 at 14:23
  • 1
    my bad yes the question was about scala, I switch between the two so I get confused – Neilos Jan 14 '15 at 19:41
  • Thank you so much! This saved me a lot of time, indeed – and I lost already many hours. I didn't have the double package problem but something similar: it is highly annoying that in Scala IDE files are shown in packages that they are not declared for, e.g. one could easily place a file marked as package name.me.test in the "visible" package in the IDE name.test, which will – somewhere (feels arbitrary) – show an error "… not found". – Majakovskij Feb 13 '15 at 03:42
  • Scala and its development ecosystem is weird. – Mohan Narayanaswamy Sep 23 '16 at 06:40
  • This happened to me today. Maybe it should just be an error? having a double package declaration make all your package references relative is probably not the most helpful thing scala could do. Thanks for your resolution. – Ion Freeman Aug 30 '19 at 20:57
  • this says something about the current state of affairs in software engineering - if you are using `vscode` and `metals`, make sure to "Clean compile workspace" and it should be all back to working order, thanks for posting – jfaleiro Aug 16 '22 at 14:55
5

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

2

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.

bbarker
  • 11,636
  • 9
  • 38
  • 62
1

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.

panther
  • 767
  • 5
  • 21
1

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

Dima Ogurtsov
  • 1,487
  • 1
  • 10
  • 18
1

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.

AlphaCR
  • 806
  • 2
  • 9
  • 23
0

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.

Adair
  • 1,697
  • 18
  • 22
0

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.

MrSpaceman
  • 404
  • 6
  • 19
0

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.