IntelliJ Run Configuration - Running your Scala application

By Nadim Bahadoor | Last updated: March 16, 2018 at 11:07 am

Overview

In this tutorial, I will explain in more detail the run configurations that are made available in IntelliJ when we either run our Scala applications.

 

Steps

1. Open IntelliJ and create a new package and Scala object
If you do not have IntelliJ installed or do not know how to create a package or Scala object, feel free to review the previous tutorials.

 

Let us name our package com.allaboutscala.chapter.one.tutorial_06 and also create a new Scala object named HelloWorldWithArguments as follows:

 

scala_arguments_1

 

NOTE:

  • As explained in the previous tutorials, our HelloWorldWithArguments application extends the App trait and as such we do not have to manually write up the main function.

2. Print command line arguments
In our HelloWorldWithArguments, we can add the following print lines to show any command line arguments that were passed in when our Scala application starts:

 

scala_arguments_2

 

NOTE:

  • Since we extended the App trait, we have access to the args variable which holds any start up arguments for our Scala application.
  • The args variable is an Array of type String and we are leveraging the mkString function to create a command delimited String for our command line arguments.

3. Run HelloWorldWithArguments
To run our Scala application, right click anywhere in HelloWorldWithArguments and select Run HelloWorldWithArguments as follows:

 

scala_arguments_2 scala_arguments_3

 

In the console window, we can see our HelloWorldWithArguments Scala application ran with the following output:

 

scala_arguments_4

 

NOTE:

  • Our command line arguments were empty! Well, that's expected since we did not pass any arguments when starting our HelloWorldWithArguments Scala application.

4. Add command line arguments to run configuration
From IntelliJ's Run menu, select Edit Configurations as follows:

 

scala_arguments_5

 

This opens up the Run/Debug Configurations windows. In the program arguments textbox, add firstArgument secondArgument thirdArgument as shown below:

 

scala_arguments_6

 

NOTE:

  • Our Run configuration has a name which defaults to the name of our Scala file. 
  • The Main class textbox has the fully qualified path for our HelloWorldWithArguments file. A fully qualified path is simply the full package name where our file resides followed by the file name itself. In our example, it is com.allaboutscala.chapter.one.tutorial_06.HelloWorldWIthArguments
  • The classpath of module defaults to our allaboutscala project which tells IntelliJ to look for our main class in this specific project.

Next click the OK button to save and close the dialogue.

 

5. Run HelloWorldWithArguments again
Instead of right click, click the play button on the top right corner of your IntelliJ.

scala_arguments_7

 

NOTE:

  • We can see the name for our run configuration which matches our file HelloWorldWithArguments.

After you've hit the play button as shown above, you should see the results of our HelloWorldWithArguments Scala application which now prints the command line arguments:

 

scala_arguments_8

 

This concludes our tutorial on IntelliJ Run Configuration - Running your 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 print command line arguments.
  • How to edit IntelliJ's run configuration.
  • How to add command line arguments on start up of a Scala application.

Tip

  • Access a specific command line argument
    Since the args variable which we inherited from the App trait is an Array of type String, we can access say the first argument only using the following syntax: args(0)

Source Code

The source code is available on the allaboutscala GitHub repository.

 

What's Next

In the next tutorial, I will go over how to debug a Scala application using IntelliJ.

Nadim Bahadoor on FacebookNadim Bahadoor on GithubNadim Bahadoor on LinkedinNadim Bahadoor on Twitter
Nadim Bahadoor
Technology and Finance Consultant with over 14 years of hands-on experience building large scale systems in the Financial (Electronic Trading Platforms), Risk, Insurance and Life Science sectors. I am self-driven and passionate about Finance, Distributed Systems, Functional Programming, Big Data, Semantic Data (Graph) and Machine Learning.
Other allaboutscala.com tutorials you may like:

Share this article on