Your first Scala Hello World application

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

Overview

In this tutorial, we will go over how to use IntelliJ IDE to create your first Scala application. If you have done previous programming in other languages, you will certainly have come across the infamous Hello World program.

 

So, without wasting any time, let's get started with  our Hello World Scala application.

 

Steps

1. Start IntelliJ
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

 

2. Create new project
From Step 1, you will now see the IntelliJ welcome screen, so go ahead and click menu "Create New Project".

 

hello_world_1

 

Next, click on Scala in the left panel and then click on SBT in the right panel. Next, click on the Next button in the bottom right corner.

 

hello_world_2

 

Enter your project name and location which in this tutorial will be called: allaboutscala

 

hello_world_3

 

At the bottom of the screen, make sure the following are checked:

  • Auto-import
  • Create directories for empty content roots automatically
  • Sources
  • Source for SBT and plugins

hello_world_4

 

At this point, do NOT hit finish button yet! But don't worry we are nearly there :) Because it's your first Scala project, you have to tell IntelliJ which JDK to use as shown in Step 3 below.

 

NOTE:

  • If you do not have a JDK installed, you can follow the instructions from the tutorial on Installing JDK.

3. Setup JDK
Click on the New button which is located alongside the "Project SDK" textbox.

 

hello_world_5

 

A new window will open which should show you the location of the JDK. Check that the path is correct and hit the OK button.

 

hello_world_6

 

You should now have a JDK set for your project. Go ahead and hit the finish button.

 

hello_world_7

 

4. Create first package

You should now see a Project panel on the left hand side which shows the project structure for your Scala project named "allaboutscala".

 

Next expand the folder "src", then "main" and then right click on the "scala" folder. A popup menu will open where you can go ahead and select "New"  and then click on "Package"

 

hello_world_8

 

You will see a popup textbox and type in "com.allaboutscala.chapter.one.tutorial_04" and click the OK button.

 

hello_world_9

 

In the Project Panel, you should now see your package created as follows:

 

hello_world_10

 

NOTE:

  • If you are new to programming, think of a package as being a folder structure where you will arrange your code base.
  • The general convention is to reverse the domain of your company. As an example, for www.allaboutscala.com, I have prefixed my package structure in reverse order as "com.allaboutscala"

 

5. Create HelloWorld object

Next, right click on the package which you created in Step 4 and select "New" and then "Scala class".

 

hello_world_11

 

A popup window will open and type in "HelloWorld" in the Name textbox. Then, select "Object" from the Kind drop down and click the OK button.

 

hello_world_12

 

On the right panel, you should now see a "HelloWorld.scala" file created for you. At the very top of the file you will see the package name which we created in Step 4.

 

In the next line, right after object HelloWorld, type in extends App as follows:

 

hello_world_13

 

Then, inside our HelloWorld file, following the first curly braces, type in println("Hello World from Scala!")

 

hello_world_14

 

NOTE:

  • In Scala, there is NO need to terminate the statement with a semi-colon as you would do in say Java or .NET!




6. Run HelloWorld
Next, it is time to run your first Scala application. Right click anywhere in the "HelloWorld.scala" file and select "Run HelloWorld"

 

hello_world_15

 

In the bottom panel, you will see that IntelliJ will first compile your code using the Scala compiler. Finally, you should see a new panel open with the result of your application which simply prints "Hello World from Scala!"

 

hello_world_16

 

And that's it! You've just created and ran your first Scala application within IntelliJ.

 

This concludes our tutorial on Your first Scala Hello World 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 create a Scala project in IntelliJ.
  • How to setup a JDK for your Scala project in IntelliJ.
  • How to run your Scala application within IntelliJ.

Tip

  • If you are new to Scala, feel free to review the What is Scala? tutorial which outlines why the JDK is required for running Scala applications.

Source Code

The source code is available on the allaboutscala GitHub repository.

 

What's Next

In the next tutorial, I will go over in a bit more detail how to launch Scala applicaitons.

 

Stay tuned!

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

  • Divus Iuilius

    Error: Could not find or load main class com.allaboutscala.ch.one.tut_04.HelloWorld

    Process finished with exit code 1

    This was the response from intellij IDEA IDE error. Any ideas why?

    ====================================================================

    package com.allaboutscala.ch.one.tut_04

    /**
    * Created by ` on 5/21/2017.
    **/
    object HelloWorld extends App {

    println(“Hello World from Scala!”)

    }

    • allaboutscala

      Hi Divus,

      The error seems to indicate some mis-configurations with your IntelliJ IDEA. Do you have JDK installed and visible in your project? Do you also have the Scala plugin installed? You can test both of these by creating a new Scala/SBT project with a simple main class similar to the HelloWorld example and see if you are able to run it.

      Cheers,
      Nadim

      • Divus Iuilius

        In order to get the “Hello World” snippet to run in IntelliJ as an object, I simply wrote the “main def(args: Array [String]” func in. And I guess that not the point. I’m sure that I have some kind of Miss-Configuration issue. Just don’t tell my wife! A little jokie-joke. In the run menu there’s an “Edit Configuration”. I did a little investigating and found nothing. In the Main class entry window it says, “com.allaboutscala.ch.one.tut_04.HelloWorld”. Does that sound right to you? Where should I be looking to see if my config is congruent with you instructions?

        My other question is, if I have the Scala and the SBT option to choose from, then the Scala plugin is already installed, Yes? I follow the instruction to the letter twice and got the same results.

        • allaboutscala

          Hi Divus,

          Since you are able to run a main, I would think that the issue may be in the way you’ve imported the project. From IntelliJ, select File-> Project from Existing Sources, and then select the “build.sbt” file in the sub-folder named allaboutscala. Hope this helps.

          Cheers,
          Nadim https://uploads.disquscdn.com/images/d170c17484c42cefff9ec471a267eb51621cafe197d06605bf7aa64dc947a365.jpg

          • Divus Iuilius

            Thank You! after careful examination, I got it working. Do you cover the process of turning Scala code into a Runnable Jar file?

          • allaboutscala

            Hi Divus, well done in getting the examples to work. Thanks also the the feedback/question on packaging jar. I will add some code snippets under the sbt section: http://allaboutscala.com/scala-frameworks/sbt/. -Cheers,Nadim.