Various ways to print “Hello world” in Scala

1. Introduction

I will show the way to build and run program which prints “hello world” without sbt, with sbt, with maven. Here, sbt is “Scala Build Tool”.

2. Preliminaries

Scala Version:2.12

sbt:1.2.7

OS:CentOS7

Java:OpenJDK8

Maven:3.6.3

3. Install Scala and Maven

Refer to [3]

4. Hello world without sbt

Execute the following procedures[1].

(1) Create source file.

vi Hello.scala
--Hello.scala
object Hello {
    def main(args: Array[String]) = {
        println("Hello, world")
    }
}
--

(2) compile

scalac Hello.scala

(3) run

scala Hello

5. Hello world with sbt

5.1 Install sbt

Execute the following commands[4].

curl -L https://www.scala-sbt.org/sbt-rpm.repo > sbt-rpm.repo
sudo mv sbt-rpm.repo /etc/yum.repos.d/
sudo yum install sbt

5.2 Create project

Execute the following command[4]. Let the project name be “hello”.

sbt new sbt/scala-seed.g8
cd hello

5.3 Hello world with sbt shell

(1) Compile

sbt compile

(2) Run

sbt run

5.4 Hello World with scala

pushd ./target/scala-2.12/classes/
scala example.Hello
popd

5.4 Hello world with java

(1) Create “plugins.sbt”[7][8]. sbt-assembly is a plugin which create a jar from a project.

vi ./project/plugins.sbt
--
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.10")
--

(2) Compile

sbt assembly

(3) Run

java -jar ./target/scala-2.12/hello-assembly-0.1.0-SNAPSHOT.jar

6. Hello world with maven

Perform the following steps. For detail, refer to [8].

(1) Create a maven project.

mvn archetype:generate -DarchetypeGroupId=net.alchim31.maven -DarchetypeArtifactId=scala-archetype-simple

(2) Maven ask you some inputs. I put the followings.

namevalue
groupidgroup1
artifactIdartifact1
version1.0
packagepackage1

(3) Add the following <plugin>…</plugin> part to pom.xml for creating jar file.

<project ...>
    <modelVersion>X.X.X</modelVersion>
    ...
    <licenses>
        ...
    </licenses>

    <properties>
        ...
    </properties>

    <dependencies>
        ...
    </dependencies>

    <build>
        ...
        <plugins>
            ...
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>com.your-package.MainClass</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

(4) Create jar file.

mvn package

(5) Execute jar.

java -jar target/artifact1-1.0-jar-with-dependencies.jar

7. References

[1] SCALA BOOK, HELLO, WORLD

https://docs.scala-lang.org/overviews/scala-book/hello-world-1.html

[2] Scala Build Tool(sbt)

https://docs.scala-lang.org/overviews/scala-book/scala-build-tool-sbt.html

[3] Installing Apache Spark from source

[4] sbt Reference Manual, Hello, World

https://www.scala-sbt.org/1.x/docs/Hello.html

[5] sbt Reference Manual, Installing sbt on Linux

https://www.scala-sbt.org/1.x/docs/Installing-sbt-on-Linux.html

[6] Custom Package Formats

https://www.scala-sbt.org/sbt-native-packager/recipes/custom.html

[7] sbt-assembly’s repository in Github, unresolved dependency: com.eed3si9n#sbt-assembly;0.14.4: not found

https://github.com/sbt/sbt-assembly/issues/239

[8] SCALA WITH MAVEN
https://docs.scala-lang.org/tutorials/scala-with-maven.html

Published by ktke109

I love open souce database management systems.