"is not a member of package" error when importing package in Scala with SBT - scala

(Relative beginner here, please be gentle...)
I've got a Scala program that I can build with sbt. I can (from within sbt) run compile and test-compile with no errors. I've defined a package by putting package com.mycompany.mypackagename at the top of several .scala files. When I do console to get a Scala REPL, this happens:
scala> import com.mycompany.mypackagename._
<console>:5: error: value mypackagename is not a member of package com.mycompany
import com.mycompany.mypackagename._
Any variation of this also fails. When I just do import com.mycompany I get no problems.
I thought that running the Scala console from within sbt would properly set the classpath based on the current projects? What (completely obvious) thing am I missing?

I ran into this same problem, and then I realized I was running scala 2.10.0 on commandline, and IDEA was using Scala 2.9.2. So the fix was to change both to use the same version, and:
sbt clean

What will happen if you import actual class name instead of wildcard.
import com.mycompany.mypackagename.ActualClassName

Related

Scala can not load file in the interpreter

I am doing the Scala course from Coursera; currently, I am at the week 2 exercises. I want to load the code into the interpreter so I can check the methods I implemented like this:
:load FunSets.scala
However, I get the following error:
<console>:10: error: not found: value common
import common._
This appears because the source file imports another package like this:
package funsets
import common._
How can I make the interpreter see the other package as well?
Is there a way of importing the entire project?
Assuming your project uses sbt, you should be able to do the following.
From the root of your project, type sbt and press enter. Your project will be loaded in sbt.
Use the console task to load the REPL with all compiled classes and libraries. Use the consoleProject task to load the REPL with access to the project definition and sbt.
The sbt documentation has more details.

import scala.io.StdIn

I'm using Eclipse ScalaIDE and for some reason I'm not able to
import scala.io.StdIn
I'm getting a red squiggly that tells me:
object StdIn is not a member of package io
And I'm seeing that it's not in that scala.io jar file. The ScalaDoc, however says it should be there. I've tried both scala 2.10.4 and 2.11.5. I've used the Eclipse ScalaIDE to create the scala project and I've also created an sbt eclipse project directly using the scalasbt.plugin which I use all the time to manage ScalaIDE dependencies.
sbt "eclipse with-source=true"
Neither way is getting it.
I'm currently taking the Coursera Reactive Programming course and an assignment file has this import. I'm able do compile the project with sbt directly, but Eclipse ScalaIDE is not doing the job. Any clues? There may be good reason why not to use scala.io.StdIn, but my question is why can I not get it to import in the ScalaIDE?
thank you
scala.io.StdIn is new in scala 2.11.x and does not exist in previous versions.
The problem you are likely encountering is that ScalaIDE is not picking up the scala version you are specifying. Since you say that you tried it with 2.10.4, it probably still has that cached or set somewhere and it's failing because it cannot find the specified class.

import scala.swing._ in eclipse giving an error that it is not a member of package scala

Does anybody have any experience with trying to get swing to work in eclipse, I can import it fine into scala using the command line interface, but when i try and use it in an eclipse scala project I get the following error:
import scala.swing._
"object swing is not a member of package scala"
Any help would be much appreciated.
If you are missing the library (as Rüdiger said, it is a separate dependency now), you can find how to add it to your build system here (for Scala 2.11):
http://search.maven.org/#artifactdetails|org.scala-lang.modules|scala-swing_2.11|1.0.1|bundle
Look under "Dependency Information", e.g. for sbt if you use that.

Can't import with IntelliJ SBT console

I installed Intellij's official SBT plugin (still in alpha), I imported without a problem a Scala SBT project (with build.sbt). But when I try to import something in the Scala console it prints this:
<scala> import recfun.Main._
<console>:7: error: not found: value recfun
import recfun.Main._
But when I launch exactly the same command with SBT running in the terminal it works fine.
What is the problem?
I found the following helped. I was working on a program imported using the SBT plugin, that had multiple sub-projects. This may also work if you have a native IntelliJ project with multiple modules.
Go to the menu "run -> Edit configurations ...", select Scala Console, and then in the box that says "Use classpath and SDK of module", pick the sub-project that has the build.sbt with the import statements you need (in my case server):
The import appeared to work after that.

Scala REPL unable to import packge

I'm trying to import com.lambdaworks.crypto.SCryptUtil (from crypto) in the Scala REPL. I'm running the REPL from the Java directory containing com/lambdaworks/crypto.
The REPL can't find com.lambdaworks.crypto.SCryptUtil, but it can autocomplete up to com.lambdaworks.crypto but can't find anything after that.
When I used the REPL in the IntelliJ IDEA after including the package in my project, I was able to find the SCryptUtil class.
Am I missing some classpath parameters that are required for import?
The REPL won't compile the Java code for you—it's only autocompleting that far because it's aware of the directory structure, but once it gets to the crypto directory it won't find any class files.
You can see this more dramatically by moving up a directory and opening a new REPL—you'll be able to autocomplete import java.com.lambdaworks.crypto, even though that's obviously not a real package hierarchy.
In this case you can move to the project root, run mvn compile to compile the Java code, and then start the REPL like this (still in the project root):
scala -classpath target/classes
Now you can import com.lambdaworks.crypto.SCryptUtil.
This only works because the project doesn't have any runtime dependencies, though—in other cases you may need either to add other things to the classpath, to build a JAR with the dependencies baked in (e.g. with the Maven Assembly plugin), or to use the mvn scala:console goal of the Maven Scala plugin.