Cannot run scala in IntelliJ - scala

I'm struggling with running Scala Hello World project using IntelliJ IDEA Scala plugin. The project is created without any problems but when I create function which should be run, the run button doesn't appear. I've got no idea why it doesn't work.
Version of IntelliJ - 2020.3.1 (Community Edition)
Version of Scala plugin 2020.3.18
I've created Scala/sbt project (I'd also tried Scala/IDEA)
Vanilla options (but I'd also tried other versions of JDK, Scala and sbt)
The run button is missing
My code is:
class Hello extends App {
println("Hello world")
}
I've tried creating Scala worksheet and It works.

When you extend App, it needs to be as object Main extends App, not class Main extends App.
See Scala 2.13 specification 9.5 (emphasis mine):
A program is a top-level object that has a member method main of type (Array[String])Unit
The main method of a program can be directly defined in the object, or it can be inherited. The scala library defines a special class scala.App whose body acts as a main method

As Suma mentioned in his answer, you should use object, and not class. The code of the Hello file should be:
object Hello {
def main(args: Array[String]): Unit = {
println("Hello world")
}
}
As stated in YOUR FIRST LINES OF SCALA. Please note that if you prefer to extend App, all of the statements will be executed, as stated in the last link:
The argument of the scala command has to be a top-level object. If that object extends trait scala.App, then all statements contained in that object will be executed; otherwise you have to add a method main which will act as the entry point of your program.
Which means you have another option:
object Hello extends App {
println("Hello world")
}
I have created a gif that shows an example.

Related

Object extends App or Object + def main(...)? [duplicate]

What is the difference between
object Application extends App {
println("Hello World")
}
and
object Application {
def main(args: Array[String]): Unit = {
println("Hello World");
}
}
The App trait is a convenient way of creating an executable scala program. The difference to the main method altenative is (apart from the obvious syntactic differences) that the App trait uses the delayed initalization feature.
From the release notes for 2.9 (see http://www.scala-lang.org/old/node/9483 )
Objects inheriting the App trait instead make use of Scala 2.9’s delayed initialization feature to execute the whole body as part of an inherited main method.
Another new feature of the App scheme is that command line arguments are now accessible via the args value (which is inherited from trait App)
These two cases is not same on the scala scripting.
object extends App was not executed by "scala MyObject.scala" command,
but the object containing the main method was executed by "scala MyObject.scala" command.
Which was described as scala looking for object with main method for scripting.
When using REPL or scala workseet of Eclipse,
need to call MyObject.main(Array[String]()) explicitly for both cases.
This simple tip be helpful for beginner like me.
App trait is implemented using the [[DelayedInit]] functionality, which means that fields of the object will not have been initialized before the main method has been executed.

How come "println" without being enclosed in a method, allowed inside a scala class?

I'm new to Scala and in Scala I found that one can write println directly inside a class despite it not being a variable or method, how is this possible?
Even if println is a method then why aren't we defining it in the class as required generally for methods? Why despite being a method it is not allowed directly in Java class but allowed in Scala?
E.g.
class Hero {
println("running fine...") // Why is this println allowed in scala when its not inside a function?
}
object MainObject{
def main(args: Array[String]){
new Hero()
}
}
Basically, it runs inside the Class constructor. Any code inside the Class besides the variable and function declarations is constructor's code.
You can write any statement directly inside a class and it will be run whenever the class is instantiated.
When you open up an editor and start typing code, you will find, that some things are already available and some need to be imported. So all of these things which are available without importing are present in the scala.Predef package.
println is one of the methods made available by the scala compiler in the scala.Predef package.
def println() = Console.println()
If you check the scala docs, println method internally calls the Console object to print the contents on the screen.
I'm new to Scala and in Scala I found that one can write println directly inside a class despite it not being a variable or method, how is this possible?
You are wrong: println is a method, that's why this is possible. Here is the documentation for scala.Predef.println.

Run class in Scala IDE

I just installed the Eclipse Scala IDE and imported some existing projects. However, when I want to run the classes (that contain a main def) via right click -> Run, I only get 'Run configurations...'. How can I run these Scala classes?
(I already checked that the 'Scala Nature' is added.)
You need to run an object not a class as noted by urban_racoons. so you can run either:
object MyApp{
def main(args: Array[String]): Unit = {
println("Hello World")
}
}
or
object MyApp extends App {
println("Hello World")
}
Scala can not run a class because the main method needs to be static. Which can only be created behind the scenes by the compiler from a singleton object in Scala. Create the object and "run as a Scala application" should appear in the "run" context sub menu as long as you have the Scala perspective open.
You can still access the programme's arguments using the App trait as in
object MyApp extends App {
println("The programme arguments are:")
args.foreach(println)
// the above is just shorthand for
// args.foreach(ar => println(ar))
}
It should also be noted that App is a trait. So it can be mixed in with other traits and any class, which makes it handy for rapid prototyping.

Difference between using App trait and main method in scala

What is the difference between
object Application extends App {
println("Hello World")
}
and
object Application {
def main(args: Array[String]): Unit = {
println("Hello World");
}
}
The App trait is a convenient way of creating an executable scala program. The difference to the main method altenative is (apart from the obvious syntactic differences) that the App trait uses the delayed initalization feature.
From the release notes for 2.9 (see http://www.scala-lang.org/old/node/9483 )
Objects inheriting the App trait instead make use of Scala 2.9’s delayed initialization feature to execute the whole body as part of an inherited main method.
Another new feature of the App scheme is that command line arguments are now accessible via the args value (which is inherited from trait App)
These two cases is not same on the scala scripting.
object extends App was not executed by "scala MyObject.scala" command,
but the object containing the main method was executed by "scala MyObject.scala" command.
Which was described as scala looking for object with main method for scripting.
When using REPL or scala workseet of Eclipse,
need to call MyObject.main(Array[String]()) explicitly for both cases.
This simple tip be helpful for beginner like me.
App trait is implemented using the [[DelayedInit]] functionality, which means that fields of the object will not have been initialized before the main method has been executed.

In Scala static value initialization does not appear to be happening before the main method is called

My Scala version:
Scala code runner version 2.9.0.1 -- Copyright 2002-2011, LAMP/EPFL
Given this code in a file named Static.scala:
object Main extends App {
val x: String = "Hello, World!"
override def main(args: Array[String]): Unit = {
println(x)
}
}
When I run:
scalac Static.scala && scala Main
I see:
null
instead of:
Hello, World!
I thought that x was a static because it was defined in an object, and that it would be initialized prior to the main method being called.
What am I doing wrong? Thanks!
This happens because of how you used the App trait. The App trait uses the DelayedInit feature which changes how the class body (including field initialization) is executed. The App trait includes a main method which executes the class body after binding the args field to the arguments to the program. Since you have overridden main this initialization is not happening. You should not provide a main method when using the App trait. You should either put the application code directly in the class body or not inherit from the App trait.
Try
lazy val x = "Hello World"
That should give you the results you're expecting.
You are not support to have a main method on an object extending App. If you do override it, you'd better understand how, exactly, DelayedInit works. In particular, objects extending App do not have static initialization -- that's the whole point of App.