IDEA sbt plugin can not auto import Predef - scala

My IntelliJ IDEA vaersion is 139.463.4
when i use scala plugin to create sbt project, it can not auto import Predef, for example :
object Hello {
def main(args: Array[String]) {
println("1")
}
}
the printlnfunction is red highlight and hint can not resolve the symbol println,
I remember that Predef is auto import in scala. the above code runs ok by idea.
it's a sbt plugin bug?

Try "invalidate cache" under file menu.

Related

How to declare main class for Scala in IntelliJ IDEA?

I'm trying to implement the simplest bits of coding in IntelliJ IDEA 2016.2.1 with Scala plugin, scala-sbt 0.13 and jdk-1.8. Here's the code:
object HelloWorld {
def main(args: Array[String]): Unit = {
println("Hello, world!")
}
}
It compiles with an error
Error: Could not find or load main class HelloWorld
In Run/Debug configurations the're is a warning 'Class HelloWorld not found in module ...'
Could you bring some light on that?
Right click the Scala project in IntelliJ IDE and click "Open Module Settings". Now, click "Global Libraries" and see whether Scala libraries are present there. If there is no Scala libraries, please add it.
Please refer the below snapshot.

Could not find or load main class in eclipse using maven and scala

I am using the Scala IDE.
I have created one Scala project and wrote simple scala object i.e.
object Test {
def main(args: Array[String]): Unit = {
eprintln("hi")
}}
It's running as expected.
After that I have converted my project to a maven project.
I did Clean and install.
after that if I am trying to run my scala class, I am getting below error:
Error: Could not find or load main class Test
How can I solve this?

There is "Run Configuration" but no "Run As Scala Application" in Eclipse

I have the following Scala class, I am able to run it from command line
by first typing sbt then in sbt mode. But I am not able to run it from eclipse .
I am already in scala perspective.
> run-main bcomposes.twitter.QuerySearch #IPL
package bcomposes.twitter
import twitter4j._
import collection.JavaConversions._
/**
* Gets a Twitter instance set up and ready to use.
*/
trait TwitterInstance {
val twitter = new TwitterFactory().getInstance
}
/**
* Given a command line query, search for tweets and print
* them.
*/
object QuerySearch extends TwitterInstance {
def main(args: Array[String]) {
val statuses = twitter.search(new Query(args(0))).getTweets
statuses.foreach(status => println(status.getText + "\n"))
}
}
Make sure the package name you specified under which your scala object file is added matches the package name you mentioned in your code. So, correct this and then check, "Run as Scala Application" should be available now
Your Scala file has to be a part of Scala project.
Create Scala project using Scala Project wizard, add your file to it and try again.
Try to click right button on the Scala object with main method (not on the root project element). Then the "Run as... Scala application" menu item appears.
"Run as Scala application" will show in Eclipse Scala IDE only if we extends the Scala object with 'APP' class.
Example:
package grreter
object Exp extends App{
println("Hello, New World!")
}
(Note: if you comment "extends App" then 'Run As Scala Application' will not show.)
The way I solve this problem is using maven to compile and package the project , when the maven build is correct, the "run as scala application" appear again.

How do I import a Java package in Scala?

I have created a jar-file with a compiled Java class. Now I want to import it in Scala. But it doesn't work.
The package name is se.mydomain.testproj and here is a small Scala application:
object TestCrypt {
import se.mydomain.testproj._
def main(args: Array[String]) {
println("Hello")
}
}
I use Eclipse and have added the jar file to the Build Path.
In Eclipse, I get the error: not found: value se
How can I import Java classes from a Jar file in Scala?
I do this all the time in Eclipse, so it definitely works. Do you see the jar in the Referenced Libraries of the project? Do you have the package name right?
Also, an import in the object as opposed to the top of the file should be fine.

Error in scala + dispatch

I am trying to GET a response from an API, using scala and dispatch. However, I get this error, after building. I googled for a solution, and tried cleaning, and resarting eclipse, but the error wont go away. What seems to be the problem? I use eclipse Helios (ie 3.6) and Scala v2.8.1, with Scala IDE v1.0.0.201104170033, installed from the Eclipse market.
dispatch{dispatch.type}.Http{object dispatch.Http} of type object dispatch.Http does not take parameters
This is my code.
class getList {
def main(args: Array[String]){
Http("http://foo.com/" >>> System.out)
}
}
What am I doing wrong?
What libraries have you downloaded? Are you sure the dependencies are set correctly? I tried with dispatch_http, version 8.0 for Scala 2.8.1, and it worked.
What imports are you using? I used these imports to make it work:
import dispatch.Http
import dispatch.HandlerVerbs._
Finally... class getList??? I assume this is a result of cut&pasting from actual code, but you should strive to produce a compilable example of your problem. Scala doesn't run programs from class, only from object, and it follows Java style of having classes start with an uppercase letter.
Here's the minimal code I used with SBT to get a working example.
Initializing:
~/test$ sbt
Project does not exist, create new project? (y/N/s) y
Name: test
Organization: test
Version [1.0]:
Scala version [2.7.7]: 2.8.1
sbt version [0.7.4]:
~/test$ cat project/build/TestProject.scala
import sbt._
class TestProject(info: ProjectInfo) extends DefaultProject(info) {
val dvers = "0.8.0"
val http = "net.databinder" %% "dispatch-http" % dvers
}
~/test$ cat src/main/scala/GetList.scala
import dispatch.Http
import dispatch.HandlerVerbs._
object GetList {
def main(args: Array[String]){
Http("http://foo.com/" >>> System.out)
}
}
~/test# sbt update run