Where is the main method to launch Scala application

By Nadim Bahadoor | Last updated: July 25, 2017 at 13:24 pm

Overview

In this tutorial, we will create another Hello World example similar to the last tutorial. However, in this example, we will define the main method manually similar to how you would define your entry point for a Java and or .NET application.

 

Steps

1. Open IntelliJ and allaboutscala project
By now, you must have IntelliJ installed, otherwise simply follow the steps from the previous tutorials.

On Mac, click on the IntelliJ icon in Finder which we've previously setup.

intellij_installer_5

On Windows, click on IntelliJ menu item from your Windows Start menu.

install_intellij_10

In the Welcome screen, you should see the allaboutscala project which we created from the previous Scala Hello World tutorial. Go ahead and click on the link to open it.

scala_main_1

2. Create a new package
Let us create another package to encapsulate our work for this tutorial. Right click on src/main/scala folder and select New and then Package.

scala_main_2

In the New Package dialogue enter: com.allaboutscala.chapter.one.tutorial_05

scala_main_3

NOTE:

  • Recall from the previous Scala Hello World tutorial that it is a general convention to name your package as the reverse of your company domain.

3. Create a new Scala class
Next, let's create a Scala class. Right click on the tutorial_05 package, select New and then Scala class.

scala_main_4

In the Create New Scala Class dialogue, enter HelloWorldMain for the name and select Object as the kind.

scala_main_5

NOTE:

  • The type of our file is an object similar to the last tutorial.

4. Define our main method manually
Our HelloWorldMain.scala file should be opened in the editor window. You can now define the main function as follows:

scala_main_6

NOTE:

  • The various parts of the main function from left to right are as follows:

def In Scala the def keyword is used to define a function.

main This is our function name.

(args: Array[String]) Our main function takes in a named parameter args which is an Array of String.

: Unit = In Scala, the Unit keyword is used to define a function which does not return anything. This is similar to the void keyword similar to Java or .NET

5. Running our application
To run our simple application, right click anywhere inside the HelloWorldMain.scala file and select Run HelloWorldMain.

scala_main_7

We can see our application was ran and the String Hello World from main function! is printed in the console window.

scala_main_8

6. The App trait
So in this tutorial, we created a simple Scala application but we had to define the main function manually ourselves. If you open the previous HelloWorld Scala application from the com.allaboutscla.chapter.one.tutorial_04 package, you would recall that we did not have to define any main function.

Instead, we extended the App trait as follows:

scala_main_9

NOTE:

  • The App trait encapsulates the main function and more!
  • You can see the source code by:
    On Window: press and hold the CTRL key + left click on the App trait
    On Mac: press and hold the CMD key + click on the App trait

scala_main_10

This concludes our tutorial on Where is the main method to launch Scala application and I hope you've found it useful!

Stay in touch via Facebook and Twitter for upcoming tutorials.

Don't forget to like and share this page :)

Summary

In this article, we went over the following:

  • How to open a Scala project from IntelliJ.
  • How to create a new package.
  • How to create a new Scala object.
  • How to define the main function manually.
  • How to run our Scala application which has the main function.
  • Had a brief look at the App trait. 

Tip

  • Please don't be overwhelmed by the Scala syntax as we will go over it in upcoming tutorials!
  • For now, you should have observed that just by extending the App trait, we were able to remove boiler plate code from having to define the main function. This is just the beginning of how Scala can help us write better code!

Source Code

The source code is available on the allaboutscala GitHub repository.

 

What's Next

In the next tutorial, I will go over in more detail the run configurations from IntelliJ.

Nadim Bahadoor on FacebookNadim Bahadoor on GithubNadim Bahadoor on LinkedinNadim Bahadoor on Twitter
Nadim Bahadoor
Senior Software Developer | Nephila Capital
Founder of allaboutscala.com. I have over 10 years of experience in building large scale real-time trading systems in the financial industry. Passionate about Distributed Systems, Scala, Big Data and Functional Programming. Stay in touch for upcoming tutorials!
Other allaboutscala.com tutorials you may like:

Share this article on