How to declare main class for Scala in IntelliJ IDEA? - scala

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.

Related

New scala project in Intellij - Error: Could not find or load main class

I just created a simple helloworld scala project in Intellij. My main class is simply printing Hello World -
package xx.yy.zz
object HelloWorld{
def main(args: Array[String]): Unit = {
System.out.print("Hello World!")
}
}
My project structure
scala-test
|_src
|_main
|_scala
|_xx
|_yy
|_zz
|_HelloWorld
In the project structure settings, I have "scala" as my source folder. I am able to select my main class correctly in the application run configuration I created. So far so good. After this, when I try to run my run configuration, I get the dreaded error -
Error: Could not find or load main class xx.yy.zz.HelloWorld
At this point, I have tried all the things I could find on google like invalidating cache, reloading project, switching source folder to be "src" instead of "scala" and many others that I have lost track of by now. Please help!!
I didn't investigate the reasons for this issue but as a workaround, you can compile the project via sbt/shell sbt before running via Intellij.

Facing issue while trying to enable source code in IntelliJ for scala

I am new to Scala programming and I have just started with basic programs in scala in IntelliJ IDE.
When i use the below code
package com.allaboutscala.chapter.one.tutorial_04
object HelloWorld extends App{
println("hello from hello world")
}
I wanted to see the source code of App where it uses the main method, but when i tries to see the source code and when I try to download it, I am getting error like
Sources not found: Sources for 'scala-library.jar' not found. I have attached the screenshot also.It would be great if someone could guide me here.
Since you are using sbt, you need to enable downloading of sources for your project:
open IntelliJ Preferences
search "sbt" or navigate to Preferences | Build, Execution, Deployment | Build Tools | sbt
select "Download: library sources"
refresh the sbt project

IDEA sbt plugin can not auto import Predef

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.

trouble with configuring scala project in intellij

I have been following the getting started with IntelliJ and Scala video from JetBrains and running into two problems.
I can't get it create or start a run configuration
I don't see the scala-test library as a selection under ProjectStructure-Modules-ChooseLibraries
What I've done so far is
Install Scala, add path and environment variables
Install Scala intellij plugin
Create new project set project sdk to java 1.7 and scala home to /usr/local/share/scala-2.10.3
Create an object that extends from App with a simple write line:
The one source file object
object HelloWorld{
def main(args: Array[String]) {
println("hello")
}
}
In the video they right click on the object file and can see a selection of run, but in my case I only see run as Scala Console. I can't seem to get the debugger to work and when I try to create a run configuration as an "Application" it says the src file is "not acceptable"
I'll recommend my simple project skeleton, to get you quickly up and running with Intellij, SBT and even Eclipse setup. Hope it helps!

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.