Setup Freebsd Scala Java Programming Language

Scala is a general-purpose computer programming language that supports object-oriented and functional programming styles on a larger scale. Scala is a powerful type of static programming language and is influenced by the Java programming language. Scala is a pure object-oriented programming language that also provides support for a functional programming approach. Scala programs can be converted into bytecode and can be run on the JVM (Java Virtual Machine).

One of the best similarities between Scala and Java is that you can code Scala the same way you code Java. Scala stands for Scalable language. It also provides a Javascript runtime. Scala is heavily influenced by Java and several other programming languages such as Lisp, Haskell, Pizza etc. Scala has become one of the most popular programming languages among developers and continues to develop through today's technology.

Scala is a functional object hybrid language with several strengths and advantages:
  1. Scala relies on the functional principles of Haskell and ML, without giving up the heavy burden of familiar object-oriented concepts, so beloved by Java programmers. As a result, Scala can blend the best of both worlds into a single whole, which provides significant gains without sacrificing the simplicity we've come to expect.
  2. Scala compiles to Java bytecode, meaning it runs on the JVM. In addition to your ability to continue to take full advantage of Java as a well-developed open source ecosystem, Scala can be integrated into the existing information space (environment) with zero migration effort.
  3. Scala was developed by Martin Odersky, perhaps better known in the Java community for the Pizza and GJ languages, the latter of which became a working prototype for generics in Java 5. If so, Scala carries a sense of "seriousness"; this language was not created on a whim, and it will not be abandoned.

1. Installing Scala

Scala runs on the Java programming language, so you must have JDK 1.8 or higher installed and ready to continue Scala installation. Since you are here to learn Scala, I assume you have Java installed on your system. Scala installation on Linux, Ubuntu, MacOS or any other Unix-based system is the same, so the steps below can be used for any Unix system.

As a first step, we check the Java version on your FreeBSD server, this is very useful and ensures that your FreeBSD server has Java installed.
root@ns7:~ # java -version
openjdk version "17.0.9" 2023-10-17
OpenJDK Runtime Environment (build 17.0.9+9-1)
OpenJDK 64-Bit Server VM (build 17.0.9+9-1, mixed mode, sharing)
On FreeBSD it is very difficult to install Scala with the ports system, so we just use the FreeBSD PKG package. Before we install Scala, make sure the Scala dependencies are installed. The following are the dependencies that are installed first.
root@ns7:~ # pkg install devel/coursier
root@ns7:~ # pkg install devel/sbt
root@ns7:~ # pkg install boehm-gc
After you have installed these three dependencies, we continue by installing Scala.
root@ns7:~ # pkg install lang/scala
Updating FreeBSD repository catalogue...
FreeBSD repository is up to date.
All repositories are up to date.
Checking integrity... done (0 conflicting)
The following 1 package(s) will be affected (of 0 checked):

Installed packages to be UPGRADED:
        scala: 3.1.0 -> 3.3.1

Number of packages to be upgraded: 1

The process will require 4 MiB more space.

Proceed with this action? [y/N]: y
Before you start running Scala on FreeBSD, we recommend checking the version of Scala first. This is to ensure that Scala is installed on your FreeBSD server.
root@ns7:~ # scala -version
Scala code runner version 3.3.1 -- Copyright 2002-2023, LAMP/EPFL
After completing the installation process, any IDE or text editor that supports Java can be used to write Scala Code and Run it in the IDE or Terminal by using the command.

# scalac file_name.scala
# scala class_name


2. Create a Scala Project

After you have finished installing Scala on your system, you are now ready to start your Scala programming project. Start by creating a simple “Hello World” program.

Create a text file named “HelloWorld.scala” and a folder /var/scala, using Putty and use your text editor of choice.
root@ns7:~ # mkdir -p /var/scala
root@ns7:~ # cd /var/scala
We continue by creating the file "/var/scala/HelloWorld.scala".
root@ns7:/var/scala # ee HelloWorld.scala

object HelloWorld {
  def main(args: Array[String]): Unit = {
    println("Hello, Mount Everest!")
  }
}
compile it.
root@ns7:/var/scala # scalac HelloWorld.scala
Use scala command to execute the generated bytecode.
root@ns7:/var/scala # scala HelloWorld.scala
Hello, Mount Everest!


3. Scala Project with SBT

Because SBT is a dependency of Sacala, we have installed SBT with other dependencies. Before using SBT, you should verify whether SBT has been set correctly or not. Run command.
root@ns7:/var/scala # sbt about
The command will display details about the installed SBT version. OK, now let's continue with setting up the SBT project, follow these steps.

To start an SBT project, we create an SBT directory to store all SBT files. For example, we will create an SBT directory in "/var" and we will name it "/var/sbt-project".
root@ns7:~ # mkdir -p /var/sbt-project
root@ns7:~ # cd /var/sbt-project
Initialize SBT in the directory.
root@ns7:/var/sbt-project # sbt new scala/scala-seed.g8
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
A minimal Scala project.

name [Scala Seed Project]: projectSBT

Template applied in /var/sbt-project/./projectsbt
To do its job, SBT has a file called build.sbt for project configuration. This file is located in your project's /var/sbt-project/projectsbt directory. The build.sbt file contains settings such as library dependencies, Scala version, and other project-specific configurations.

The main goal of any build tool is to compile source code. Use this command to compile SBT.
root@ns7:/var/sbt-project # cd projectsbt
root@ns7:/var/sbt-project/projectsbt # sbt compile
Once your project is ready, we will package it. Use the package command to create a JAR file.
root@ns7:/var/sbt-project/projectsbt # sbt package
The above command will bundle your compiled code into a JAR file. The above command will bundle your compiled code into a JAR file. After that, continue with the "sbt clean" command.

Testing is an important part of any project. With SBT, you can run tests by using the "sbt test" command.
root@ns7:/var/sbt-project/projectsbt # sbt test
The final step is running the SBT project.
root@ns7:/var/sbt-project/projectsbt # sbt run
[info] welcome to sbt 1.9.7 (OpenJDK BSD Porting Team Java 17.0.9)
[info] loading project definition from /var/sbt-project/projectsbt/project
[info] loading settings for project root from build.sbt ...
[info] set current project to projectSBT (in build file:/var/sbt-project/projectsbt/)
[info] compiling 1 Scala source to /var/sbt-project/projectsbt/target/scala-2.13/classes ...
[info] running example.Hello
hello
[success] Total time: 7 s, completed Jan 1, 2024, 9:12:42 PM


4. Scala Project with Maven

To create a Scala project with Maven, make sure you have Maven installed on your FreeBSD server. You can read our previous article about how to install Maven on FreeBSD.


Maven is a management tool for creating complete projects. It favors convention over configuration, this greatly simplifies building for "standard" projects and Maven users can usually understand the structure of another Maven project just by looking at the pom.xml (Project Object Model).

If everything goes well, you should now have Maven and Scala support on your FreeBSD server. Let's create a new Scala project with maven, starting with a simple Scala project based on archetypes.

Run the following command to create a maven project.
root@ns7:/ # cd var
root@ns7:/var # mvn archetype:generate -DgroupId=com.unixwinbsd.blogapp -DartifactId=java-blog-project -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
We save the Maven project in the "/var/java-blog-project" folder.

We open the "/var/java-blog-project/pom.xml" file, and delete all its contents, then replace it with the script below.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.unixwinbsd</groupId>
    <artifactId>java-blog-project</artifactId>
    <version>1.0.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

        <java.version>1.8</java.version>
        <scala.version>2.12.6</scala.version>
    </properties>

    <dependencies>
        <!-- scala-maven-plugin determines the Scala version to use from this dependency -->
        <dependency>
            <groupId>org.scala-lang</groupId>
            <artifactId>scala-library</artifactId>
            <version>${scala.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- Configure maven-compiler-plugin to use the desired Java version -->
            <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.unixwinbsd.blogapp.HelloScala</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>


            <!-- Use build-helper-maven-plugin to add Scala source and test source directories -->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <id>add-source</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>add-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>src/main/scala</source>
                            </sources>
                        </configuration>
                    </execution>
                    <execution>
                        <id>add-test-source</id>
                        <phase>generate-test-sources</phase>
                        <goals>
                            <goal>add-test-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>src/test/scala</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!-- Use scala-maven-plugin for Scala support -->
            <plugin>
                <groupId>net.alchim31.maven</groupId>
                <artifactId>scala-maven-plugin</artifactId>
                <version>3.2.2</version>
                <executions>
                    <execution>
                        <goals>
                            <!-- Need to specify this explicitly, otherwise plugin won't be called when doing e.g. mvn compile -->
                            <goal>compile</goal>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <!-- scala assembly-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>


Create a new folder in your maven project.
root@ns7:/var # cd /var/java-blog-project/src/main
root@ns7:/var/java-blog-project/src/main # mkdir -p scala/com/unixwinbsd/blogapp
Create the file HelloScala.scala.
root@ns7:/var/java-blog-project/src/main # cd scala/com/unixwinbsd/blogapp
root@ns7:/var/java-blog-project/src/main/scala/com/unixwinbsd/blogapp # touch HelloScala.scala
root@ns7:/var/java-blog-project/src/main/scala/com/unixwinbsd/blogapp # chmod +x HelloScala.scala
In the "/var/java-blog-project/src/main/scala/com/unixwinbsd/blogapp/HelloScala.scala" file, enter the following script.

package com.unixwinbsd.blogapp;

object HelloScala extends App {
  println("Hello FreeBSD Scala")
}

We carry out the compile, package and install commands on the Maven project that we have created.
root@ns7:/var/java-blog-project/src/main/scala/com/unixwinbsd/blogapp # cd /var/java-blog-project
root@ns7:/var/java-blog-project # mvn compile && mvn package && mvn install
Before we run the Maven Scala project, let's look at the "target" folder.
root@ns7:/var/java-blog-project # cd target
root@ns7:/var/java-blog-project/target # ls
archive-tmp                                                     java-blog-project-1.0.0-SNAPSHOT-jar-with-dependencies.jar
classes                                                         java-blog-project-1.0.0-SNAPSHOT.jar
classes.-1515151658.timestamp                                   maven-archiver
generated-sources                                               maven-status
The JAR executable file is "java-blog-project-1.0.0-SNAPSHOT-jar-with-dependencies.jar".

As the final step, we run the Maven Scala project.
root@ns7:/var/java-blog-project/target # java -jar java-blog-project-1.0.0-SNAPSHOT-jar-with-dependencies.jar
Hello FreeBSD Scala
With "Hello FreeBSD Scala" display. you have successfully created a Scala project with Maven.

Happy! You have successfully installed Scala with SBT and Maven. You are now ready to start exploring the powerful features and flexibility that Scala has to offer. Happy coding!.
Iwan Setiawan

I Like Adventure: Mahameru Mount, Rinjani Mount I Like Writer FreeBSD

Post a Comment

Previous Post Next Post