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.
Related
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
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")
}
}
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")
}
}
Apologies as this is a newby question. I have the following SBT project containing a scala object. When I run 'Hi' I get "Class not found" exception. Where to specify the class path?
click here to see the screen shot
You need to delete this object and create a new one. Right click on src->main->scala(NOT in target folder) folder new -> Scala class -> type "Hi" in name field in check box choose object. And paste your:
def main(args: Array[String]) = println("hi")
This could be because main method doesn't have return type, try adding Unit so I must be like this:
def main(args:Array[String]):Unit = println("hi")
regards
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.