Scala can not load file in the interpreter - scala

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.

Related

How to import an user defined package into scala REPL automatically when scala REPL is started?

When scala REPL starts some default packages like
scala.lang._ ,
scala.Predef are automatically available.Suppose I have my own package like com.raghhuraamm.rUtils._
How to import this package automatically when REPL starts? Is there a
way or I just have to type "import com.raghhuraamm.rUtils._ " every
time I start scala REPL?
If you could use sbt console to launch the REPL, you can create a build.sbt containing this line:
initialCommands in Compile in console += "import com.raghhuraamm.rUtils._"
Source: https://www.scala-sbt.org/1.x/docs/Inspecting-Settings.html
Create a script (myPreload.scala, say) that contains all the imports that you want:
// in myPreload.scala
import com.raghhuraamm.rUtils._
Assuming that the classes are packaged in my.jar, start the scala repl as follows:
scala -cp path/to/my.jar -i some/other/path/to/myPreload.scala

Making jar files available to the scala REPL

I can't seem to get this to work, so i have tried what's probably the most straightforward case--ie, retrieve the jar file and into a given directory then start the scala REPL from that directory--still no luck.
I know the jar files are fine and correctly references (eg, i can access them from project directories via eclipse/scala ide)
I also know that my import statements (one is shown below) are valid.
cd ~/my_scala_jars
retrieve the artifact from the maven repo
wget -q -nd -np -r \
http://repo1.maven.org/maven2/org/scalaz/scalaz-core_2.12.0-M1/7.1.2/scalaz-core_2.12.0-M1-7.1.2.jar
start the scala REPL and add the jar to the REPL classpath:
scala -cp scalaz-core_2.12.0-M1-7.1.2.jar
scala> import scalaz.stream.io
<console>:7: error: object stream is not a member of package scalaz
import scalaz.stream.io
in case it's useful, trying this with different jars (that i downloaded as i showed with scalaz above) will sometimes cause the REPL to throw a not found error instead
scala> import breeze.linalg._
<console>:7: error: not found: value breeze
import breeze.linalg._
it seems to me that the REPL should recognize these jars because the directory has been added to my CLASSPATH, but also because i believe the current directory is also added to the classpath (at least during the REPL session)--and still both fail.
I am not sure why the jar files are not picked up as I haven't really tried to do that. What I do, and find useful, is to create a number of build.sbt files for specific purposes, e.g. for working with a specific suite of libraries at a REPL.
If you do this, and then launch a REPL using the command sbt console, from the directory containing the appropriate build.sbt, then you can have a console with the libraries you want available on the classpath to that console.
This might not be ideal, but it does work and I find it quite useful.

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.

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

(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