Not found object error when importing external library in intellij - scala

Here is my sbt file myproject/build.sbt
version := "1.0"
scalaVersion := "2.12.1"
libraryDependencies ++= Seq(
"com.typesafe.akka" %% "akka-actor" % "2.4.16",
"io.circe" %% "circe-core" % "0.6.1",
"io.circe" %% "circe-generic" % "0.6.1",
"io.circe" %% "circe-parser" % "0.6.1"
)
Here is my scala file myproject/src/test.scala
package mytest
import akka._
object test {
def main(args: Array[String]) {
print(2)
}
}
I verified that my external library contains, akka
but intellij keep saying that
Error:(7, 8) not found: object akka
import akka._
I am using intellij community edition 2016.3 with the latest scala plugin (which should include latest sbt)
Can someone give me a hint on how to resolve this?

To fix the problem, you have to place your Scala source file into src/main/scala directory. Otherwise IntelliJ/SBT can't recognize it as file related to the project, so it can't associate project dependencies with it.
By default Scala source files can be placed either in the root directory of your project, or in src/main/scala (for main sources, there is also src/test/scala for tests).
If you want to use some other directories to store your Scala source files, you can configure it this way in your build.sbt:
sourceDirectories in Compile += new File("src")

I had a similar problem and it was nothing to do with the directory structure in my case. IntelliJ asks you to refresh when you add a new dependency in build.sbt. I also manually refreshed it form the SBT Shell and still same error.
In the end I closed the project and re-opened and it was fixed.

Related

object mockito is not a member of package org

Relatively new to sbt and Mockito.
I want to use Mockito in tests, but I'm getting errors related to the Mockito imports when I compile the tests
Imports in test file:
import org.scalatest._
import org.mockito.Mockito._
import org.scalatest.mockito.MockitoSugar
sbt file:
name := "blah"
version := "0.1"
scalaVersion := "2.13.0"
libraryDependencies += "org.scalactic" %% "scalactic" % "3.0.8"
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.8" % "test"
libraryDependencies += "org.mockito" % "mockito-core" % "1.8.5" % "test"
I get these error messages when the tests (fail to) compile:
object mockito is not a member of package org [error] import org.mockito.Mockito._
and also:
Symbol 'type org.mockito.MockSettings' is missing from the classpath.
[error] This symbol is required by 'value org.scalatest.mockito.MockitoSugar.mockSettings'.
I've had a play around with changing some of the versions of scalatest and mockito in the sbt file, but not really if that's getting at the root of the problem or not.
Thanks for any help!
You're using a very old version of Mockito, which is older than the one Scalates relies on, you probably need some 2.x.x version.
On the other hand, I'd recomend you to go fo mockito-scala rather than mockito-core and skip the Scalatest provided classes altogether as they are quite basic.
I suspect you have a caching problem. This happens especially with Intellij.
Here 2 ideas:
Reload the sbt project. See https://stackoverflow.com/a/20466144/2750966
Close the project / delete .idea an open the project newly with Intellij.
Let me know if it is not related with Intellij

Trying to integrate mongoDB and spark, keep having errors related to "could not find or load class"

So I've been trying to integrate mongoDB and Spark, after handling reading about all the dependencies, I'm running:
Jdk 1.8
scala-sdk-2.11-7
Spark version 2.0.2
This is how my build.sbt file looks:
scalaVersion := "2.11.7"
libraryDependencies ++= Seq(
"org.mongodb.spark" %% "mongo-spark-connector" % "2.0.0",
"org.apache.spark" %% "spark-core" % "2.0.0",
"org.apache.spark" %% "spark-sql" % "2.0.0"
)
Every time I run this simple script:
import com.mongodb.spark._
import org.apache.log4j._
object myMain {
def main(args: Array[String]): Unit = {
println("lol")
}
}
It says "could not find or load main class myMain".
All I'm trying to see is if the dependencies are fine and if the script will import the libraries and work. Please help, I've been reading about this for at least a day and can not find any concrete tips except "compatibility problems".
newProject - >
src ->
project (src-build),
build.sbt,
myMain$
This is how my project tree in IntelliJ looks, in external libraries I have all libs downloaded by build.sbt file and the scala and java dependencies like I wrote above.
Thank you!
The answer was to change from IntelliJ ide to Eclipse.

sryza/spark-timeseries: NoSuchMethodError: scala.runtime.IntRef.create(I)Lscala/runtime/IntRef;

I have a Scala project that I build with sbt. It uses the sryza/spark-timeseries library.
I am trying to run the following simple code:
val tsAirPassengers = new DenseVector(Array(
112.0,118.0,132.0,129.0,121.0,135.0,148.0,148.0,136.0,119.0,104.0,118.0,115.0,126.0,
141.0,135.0,125.0,149.0,170.0,170.0,158.0,133.0,114.0,140.0,145.0,150.0,178.0,163.0,
172.0,178.0,199.0,199.0,184.0,162.0,146.0,166.0,171.0,180.0,193.0,181.0,183.0,218.0,
230.0,242.0,209.0,191.0,172.0,194.0,196.0,196.0,236.0,235.0,229.0,243.0,264.0,272.0,
237.0,211.0,180.0,201.0,204.0,188.0,235.0,227.0,234.0,264.0,302.0,293.0,259.0,229.0,
203.0,229.0,242.0,233.0,267.0,269.0,270.0,315.0,364.0,347.0,312.0,274.0,237.0,278.0,
284.0,277.0,317.0,313.0,318.0,374.0,413.0,405.0,355.0,306.0,271.0,306.0,315.0,301.0,
356.0,348.0,355.0,422.0,465.0,467.0,404.0,347.0,305.0,336.0,340.0,318.0,362.0,348.0,
363.0,435.0,491.0,505.0,404.0,359.0,310.0,337.0,360.0,342.0,406.0,396.0,420.0,472.0,
548.0,559.0,463.0,407.0,362.0,405.0,417.0,391.0,419.0,461.0,472.0,535.0,622.0,606.0,
508.0,461.0,390.0,432.0
))
val period = 12
val model = HoltWinters.fitModel(tsAirPassengers, period, "additive", "BOBYQA")
It builds fine, but when I try to run it, I get this error:
Exception in thread "main" java.lang.NoSuchMethodError: scala.runtime.IntRef.create(I)Lscala/runtime/IntRef;
at com.cloudera.sparkts.models.HoltWintersModel.convolve(HoltWinters.scala:252)
at com.cloudera.sparkts.models.HoltWintersModel.initHoltWinters(HoltWinters.scala:277)
at com.cloudera.sparkts.models.HoltWintersModel.getHoltWintersComponents(HoltWinters.scala:190)
.
.
.
The error occurs on this line:
val model = HoltWinters.fitModel(tsAirPassengers, period, "additive", "BOBYQA")
My build.sbt includes:
name := "acme-project"
version := "0.0.1"
scalaVersion := "2.10.5"
libraryDependencies ++= Seq(
"org.apache.spark" %% "spark-hive" % "1.6.0",
"net.liftweb" %% "lift-json" % "2.5+",
"com.github.seratch" %% "awscala" % "0.3.+",
"org.apache.spark" % "spark-mllib_2.10" % "1.6.2"
)
I have placed sparkts-0.4.0-SNAPSHOT.jar in the lib folder of my project. (I would have preferred to add a libraryDependency, but spark-ts does not appear to be on Maven Central.)
What is causing this run-time error?
The library requires Scala 2.11, not 2.10, and Spark 2.0, not 1.6.2, as you can see from
<scala.minor.version>2.11</scala.minor.version>
<scala.complete.version>${scala.minor.version}.8</scala.complete.version>
<spark.version>2.0.0</spark.version>
in pom.xml. You can try changing these and seeing if it still compiles, find which older version of sparkts is compatible with your versions, or update your project's Scala and Spark versions (don't miss spark-mllib_2.10 in this case).
Also, if you put the jar into lib folder, you also have to put its dependencies there (and their dependencies, etc.) or into libraryDependencies. Instead, publish sparkts into your local repository using mvn install (IIRC) and add it to libraryDependencies, which will allow SBT to resolve its dependencies.

How to use play-plugins-mailer with Play 2.3 and Scala 2.11?

I am trying to use the play plugin for sending emails:
https://github.com/playframework/play-mailer
I have followed the instructions as found on github: added the dependency to build.sbt, created play.plugins with the specified content (do I need to register the file somehow)?
but I get a compilation error:
object mailer is not a member of package play.api.libs
when trying to import
import play.api.libs.mailer._
I get another compilation error on
val mail = use[MailerPlugin].email
MailerPlugin and use are not found.
How to get this working?
Note: the plugin is correctly downloaded (I can find it in my .ivy2 directory), but it is not listed as a dependency in my application.
My build.sbt file:
name := ...
version := "1.0-SNAPSHOT"
scalaVersion := "2.11.2"
resolvers += Resolver.typesafeRepo("releases")
//"mysql" % "mysql-connector-java" % "5.1.31"
libraryDependencies ++= Seq(
"mysql" % "mysql-connector-java" % "5.1.24",
"org.webjars" %% "webjars-play" % "2.3.0-2",
"com.typesafe.play" %% "play-slick" % "0.8.0",
"com.typesafe.play.plugins" %% "play-plugins-mailer" % "2.3.1",
"org.mindrot" % "jbcrypt" % "0.3m"
)
fork in Test := false
lazy val root = (project in file(".")).enablePlugins(PlayScala)
And my play.plugins contains only:
1500:com.typesafe.plugin.CommonsMailerPlugin
UPDATE:
I've downloaded the sample project from https://github.com/playframework/play-mailer
and tried to compile using sbt. It failed with exactly the same problem.
It looks like the problem is somehow with the version of plugin that is available in the typesafe repo:
I've built the plugin from sources, published it to my local repository and then everything compiled fine.
In the build.sbt of the sample app there is:
resolvers += Resolver.file("LocalIvy", file(Path.userHome + File.separator + ".ivy2" + File.separator + "local"))(Resolver.ivyStylePatterns)
So it looks like the authors also had problems in compiling the app using the plugin deployed to the official repository.
UPDATE:
Well, it compiled fine, but then failed at runtime with java.lang.ClassNotFoundException: com.typesafe.plugin.CommonsMailerPlugin
UPDATE 2:
The sample play.plugins is also wrong, the correct one should be:
1500:play.api.libs.mailer.CommonsMailerPlugin
and then eveyrthing finally works
The README is up to date with the latest development (upcoming version).
As of 19/12/2014, the latest version released and available in the Typesafe repository is 2.3.1. If you want to use this version you need to refer to the README in the v2.3.1 tag: https://github.com/playframework/play-mailer/tree/v2.3.1
For the upcoming version we have decided to break compatibility to move the Play mailer into its own package and provide a better implementation. That's the reason why the documentation in the master branch is not working with version 2.3.1.

Importing .jar files into Scala environment

Even after reading: Scala, problem with a jar file, I'm still a bit confused. I am trying to import some packages into my Scala file, and the interpreter is not recognizing them even after adding to classpath.
One example:
I have the import statement:
import org.json4s._
I downloaded the .jar from here: http://mvnrepository.com/artifact/org.json4s/json4s-native_2.10/3.2.4
and added to the interpreter classpath using:
scala> :cp /Users/aspangher13/Downloads/json4s-native_2.10-3.2.4.jar
Scala acknowledges the classpath:
Your new classpath is: ".:/Users/aspangher13/Downloads/json4s-native_2.10-3.2.4.jar:/Users/aspangher13/Downloads/jna-3.5.2.jar"
But still throws this error:
<console>:7: error: object scalatra is not a member of package org
import org.json4s._
Can anyone see what I'm doing wrong? Thanks!!
And as a followup, does anyone know where to find the package: JsonAST._?
Go the simple and create a little sbt project.
First step - create a project
For your purposes you don't need a complex build. So just create two files:
./build.sbt
name := "name your project"
version := "0.1"
scalaVersion := "2.10.2" // or whatever you prefer
./project/build.properties
sbt.version=0.12.4
The just go to the project root folder and call sbt
Second step - add dependencies
Open your ./build.sbt file and add:
libraryDependency ++= Seq(
"org.scalatra" %% "scalatra" % "2.2.1",
"org.scalatra" %% "scalatra-scalate" % "2.2.1",
"org.scalatra" %% "scalatra-specs2" % "2.2.1" % "test",
"org.json4s" %% "json4s-native % "3.2.4",
"net.java.dev.jna" & "jna" & "3.5.2"
)
Step three - run the console
Don't forget to reload sbt with reload task, and then call console or console-quick task. This should work.
But there are easier ways to do this:
1) Use gitter8 - Scalatra gitter8 project
2) Read little into about Scalatra sbt dependencies
Still not sure how :cp works but if you execute
scala -classpath "list of jars colon separated"
then inside the REPL do your imports it should work
import org.json4s._
import org.xyz
However, when you try to use the classes you are likely to be missing transitive dependencies required by json4s and so we come back to the sbt example # 4lex1v describes, which will handle this. Creating a little project and running sbt console will indeed greatly simplify this.
Seems like the -classpath and :cp are primarily meant to make your code available in the shell and then only if you understand all of the transitive dependencies, or have none.