Where is the main method to launch Scala application
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.
On Windows, click on IntelliJ menu item from your Windows Start menu.
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.
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.
In the New Package dialogue enter: com.allaboutscala.chapter.one.tutorial_05
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.
In the Create New Scala Class dialogue, enter HelloWorldMain for the name and select Object as the kind.
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:
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.
We can see our application was ran and the String Hello World from main function! is printed in the console window.
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:
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
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.
Share this article on