Unable to run Scala application in IntelliJ - scala

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")
}
}

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

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.

Scala class decleration

I have used Scala but I worked with objects but now I have to use classes. I am creating a scala class in a package, however, when I try to run it, it asks me to choose between Scala Applet or Scala Application and none of them work. Anybody has an idea on how to fix this problem? And do you declare a main method inside a class (like in objects) ?
For running your application you need to have App extended object or an object with main method.
It is nice that you are using classes. But the entry point of your code must be an object with main method or ( App extended )
So use your classes to build the application but start your application from an object.
case class Test(val x: Int);
object Main extends App {
override def main(args: Array[String]): Unit = {
val x = Test(5);
println("this is the main method");
println(x);
}
}
In the JVM world, the main method in the entry point to an application.
In general, the main method is where you start your various components, the threads/executors that run them etc.

Using IntelliJ for a Scala Project

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.