DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Modernizing Apache Spark Applications With GenAI: Migrating From Java to Scala
  • Mastering Advanced Aggregations in Spark SQL
  • Thermometer Continuation in Scala
  • Deploying a Scala Play Application to Heroku: A Step-by-Step Guide

Trending

  • Engineering Closed-Loop Graph-RAG Systems, Part 3: Closing the Loop in Graph-RAG Systems
  • Engineering Closed-Loop Graph-RAG Systems, Part 2: From Prompts to Rules
  • Compliance Automated Standard Solution (COMPASS), Part 11: Compliance as Code, the OSCAL MCP Server Way
  • Identity in Action
  1. DZone
  2. Coding
  3. Languages
  4. A Look at the Scala Main Class

A Look at the Scala Main Class

Check out this quick look at adding a main class in Scala.

By 
Emmanouil Gkatziouras user avatar
Emmanouil Gkatziouras
DZone Core CORE ·
Jan. 17, 20 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
9.9K Views

Join the DZone community and get the full member experience.

Join For Free

Check out this quick look at adding a main class in Scala.

Adding a main class in Scala is something that I always end up searching online, so next time, it shall be through this blog post! Let's get started.

You may also like: [DZone Refcard] Getting Started With Scala

Go for the Extends App Option

One way is to add aMainClass by extending theAppclass. Everything else that gets executed on that block is part of the "main" function.

Java




x


 
1
package com.gkatzioura
2
 
          
3
object MainClass extends App {
4
 
          
5
  println("Hello world"!)
6
}



Then, you can access the arguments since they are a variable on the App.

Java




xxxxxxxxxx
1
10
9


 
1
package com.gkatzioura
2
 
          
3
object MainClass extends App {
4
 
          
5
  for( arg <- args ) {
6
    println(arg)
7
  }
8
 
          
9
}




Add a Main Method

This is the most Java familiar option:

Java




xxxxxxxxxx
1


 
1
package com.gkatzioura
2
 
          
3
object MainClass {
4
 
          
5
  def main(args: Array[String]): Unit = {
6
    println("Hello, world!")
7
  }
8
 
          
9
}



As expected, you receive the program arguments through the function arguments.

Java




xxxxxxxxxx
1
11


 
1
package com.gkatzioura
2
 
          
3
object MainClass {
4
 
          
5
  def main(args: Array[String]): Unit = {
6
    for( arg <- args ) {
7
      println(arg)
8
    }
9
  }
10
 
          
11
}



Hope you enjoyed! Please share your thoughts and questions in the comments section!

Further Reading

[DZone Refcard] Getting Started With Scala

Scala in a Java Maven Project

Making Spring Web Services With Scala

Scala (programming language)

Published at DZone with permission of Emmanouil Gkatziouras. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Modernizing Apache Spark Applications With GenAI: Migrating From Java to Scala
  • Mastering Advanced Aggregations in Spark SQL
  • Thermometer Continuation in Scala
  • Deploying a Scala Play Application to Heroku: A Step-by-Step Guide

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook