Using IntelliJ for a Scala Project - scala

I am missing something simple here.
Scala downloaded
Scala home set
IntelliJ plugin downloaded
When new module is added, scala is chosen:
When new class is created, however, when trying to run it, i get
Looking as module properties, i see
What am i missing please?

Two things:
The file you're working with must be a Scala class.
You should have an object declared somewhere (preferably outside of the class).
object runnableObject {
def main(args: Array[String]) {
println("Hello world!")
}
}
You can then use this object to run your Scala code in IntelliJ.

Related

When and why does Scala code needs to be on a method called main inside an object?

I realized that when I code some script in scala and compile and run it from the terminal, I need to put it on a main method inside a object. But when a run it inside Intellij IDEA that's not needed. Why is that? Why do some people extend App on the global object?
In Scala there are two ways to create a runnable main class.
One is using the main method in an object:
object HelloWorld {
def main(args: Array[String]): Unit = {
println("Hello, world!")
}
}
A slightly shorter version is extending the App trait and writing the code directly into the object body:
object HelloWorld extends App {
println("Hello, world!")
}
Both of these work irrespective of whether you're using IntelliJ.
See also: https://www.scala-lang.org/documentation/your-first-lines-of-scala.html

Unable to run Scala application in IntelliJ

I'm trying to run a simple Scala snippet,
package example
class HelloWorld extends App {
println("Hello world")
}
in the IntelliJ IDE with Scala installed. However, the "Run" button appears to be grayed out, and I also don't see it in the context menu (not shown in the screen grab below).
In accordance with the answer of Unable to run Java code with Intellij IDEA, the code is in the src folder which is marked blue. (I've also tried marking it as a 'tests' folder but to no avail). What am I missing?
HelloWorld should be object, not class:
package example
object HelloWorld extends App {
println("Hello world")
}
For more info about singleton objects, you can see this chapter of "Programming in Scala" book and this question.
To run any scala application you need singleton object which will either extend the App or define the main method that takes one parameter, an Array[String], and has a result type of Unit.
Any standalone object with a main method of the proper signature can be used as the entry point into an application.
So you can run your scala application in following two ways.
package example
object HelloWorld extends App {
println("Hello world")
}
object HelloWorld {
def main(args: Array[String]) {
println("Hello world")
}
}

How to run scala class on IDEA

I have installed scala plugin in IDEA, set scala SDK and created a scala module.But I can only find "Compile" and "Run Scala Console" option.How can I run scala class like java?
change class Hello to object such as described in :
https://www.jetbrains.com/help/idea/2016.2/creating-and-running-your-scala-application.html
I has similar issue, i replaced main method in class to object Main extends App {} analogue. After idea restart i could use both variant. it's trouble of intellij.
It is a mistake! We should create a object which contains main to run scala application:
class Hello {
}
object Hello {
def main(args: Array[String]) {
println("Hello World")
}
}

Intellij Scala- unable to find Scala App

I just set up Scala in Intellij(along with SDK and JDK)
File -> Project -> Scala -> Scala
Created a project.
under project name src --> right click --> I can see 1.Scala Class , 2.Scala Worksheet , 3.Scala Script , But the Scala application or App option is not coming .
what am i doing wrong here. Please help
Just like in Java you should create a class, that has main method, that can be found by java machine and get run. The searched method has signature
In java:
public static void main(String[] args){
//your code goes here
}
In scala:
def main(args: Array[String]): Unit = {
//your code goes here
}
Also in scala you can extend from App(scala.App, all names from package scala._ are imported by default):
object Main extends App{
//your code goes here
}
That moves your code into automatically created function def main(args: Array[String]): Unit. This option is "faster", but limited in some functionality.
So, click "Scala class", pick object, add "extends App" clause or write "def main ..."
You need to have a main method in some object to make it "runnable":
object test extends App {
println("Hi");
}
or
object test {
def main(args:Array[String]):Unit = println("Hi");
}
Extending App creates a main method under the hood for you, but messes with initialization.

How do I use an unmanaged dependency in this simple Play example?

I am trying to write a Scala Play web service that returns JSON objects and am having trouble calling a function in a dependency. Can someone tell me what I'm doing wrong in this simplified example?
I have a project called SimpleJSONAPI that consists of the following object.
package com.github.wpm.SimpleJSONAPI
import play.api.libs.json.{JsValue, Json}
object SimpleJSONAPI {
def toJson(s: String): JsValue = Json.toJson(Map("value" -> s))
}
Unit tests confirm that given a string it returns a JSON object of the form {"value":"string"}.
I have a separate Play 2.2.3 Scala project that I created by typing play new PlayJSON. I added the following json action to the controller in the generated application.
package controllers
import play.api.mvc._
import com.github.wpm.SimpleJSONAPI._
object Application extends Controller {
def index = Action {
Ok(views.html.index("Your new application is ready."))
}
def json = {
val j = SimpleJSONAPI.toJson("The JSON API")
Action {
Ok(j)
}
}
}
I also added this route.
GET /json controllers.Application.json
In the root of the PlayJSON project I have a lib directory that contains the simplejsonapi_2.11.jar built by SimpleJSONAPI. This appears to contain the correct code.
> jar tf lib/simplejsonapi_2.11.jar
META-INF/MANIFEST.MF
com/
com/github/
com/github/wpm/
com/github/wpm/SimpleJSONAPI/
com/github/wpm/SimpleJSONAPI/SimpleJSONAPI$.class
com/github/wpm/SimpleJSONAPI/SimpleJSONAPI.class
This compiles, but when I try to connect to localhost:9000/json I get the following runtime error in the line with the val j assignment.
java.lang.NoSuchMethodError: scala.Predef$.ArrowAssoc(L/java/lang/Object;)Ljava/lang/Object;
I've also seen the same error in a unit test that exercises the /json route with a FakeRequest.
If I copy the toJson function from the external dependency into the Play application everything works.
As far as I can tell from the documentation I'm doing everything right, and the error message is opaque. Can someone tell me how to get this to work?
I think your import is incorrect given how you are using the API. Either exclude the object name on the import...
import com.github.wpm.SimpleJSONAPI._
Or change your usage to drop the object name...
val j = toJson("The JSON API")
This was a problem with Scala compiler version compatibility. I compiled my SimpleJSONAPI dependency with Scala 2.11, while the Play app was being built with Scala 2.10. When I changed the SimpleJSONAPI dependency to also build with Scala 2.10, I was able to use it in my Play app.
This was confusing because it's not obvious from the project files which version of Scala a Play app is using, and the error message about ArrowAssoc gives no indication that it is a compiler version issue.