IntelliJ Run Configuration - Running your Scala application
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.
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:
- 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:
- 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:
In the console window, we can see our HelloWorldWithArguments Scala application ran with the following output:
- 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:
This opens up the Run/Debug Configurations windows. In the program arguments textbox, add firstArgument secondArgument thirdArgument as shown below:
- 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.
- 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:
This concludes our tutorial on IntelliJ Run Configuration - Running your Scala application and I hope you've found it useful!
Don't forget to like and share this page :)
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.
- 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)
The source code is available on the allaboutscala GitHub repository.
In the next tutorial, I will go over how to debug a Scala application using IntelliJ.
Share this article on