InteliJ IDEA,create a Scala class,proper order - scala

I am learning from Coursera tutorial,but my IDEA is different from the one in the course.This is the screenshot
When I try to create class,I got this
What should I change to get object instead of class?
This is how my GUI looks like

When you ask to create a scala class, select the object from dropdown
1) Create scala class
2) Select kind of scala class
but you will have to add main method by yourself, or also can extend App.
object ExampleApp {
def main(args: Array[String]): Unit = {
}
}

When you open the dialog for new class, do not click "OK" and below the name of file, select dropdown "kind" to select object or traits.

Related

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.

Class Not Found running Scala in IntelliJ SBT project

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

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.

scala selenium dsl page object

I am using the Selenium dsl and would like to use a Page object. Currently however it seems I have to define the page object inside the test class. The reason I would want a page object is to share common features between tests so this seems a bit pointless... Has anyone been using the page object model with Selenium DSL? What is the idea behind defining the page object in the same class? How come I get a compiler error if I define the page object outside of the test class. Am I doing something wrong?
The compiler error I get is:
Expected MySpec.this.type#Page, actual: MyPage
You can define the class outside of the test class like this:
class TwitterPage {
val url = "http://twitter.com"
}
Then, use it inside the test by mixing in the Page trait:
val page = new TwitterPage with Page
go to page
title should be ("Welcome to Twitter")
This compiled and worked just fine for me.

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.