Scala + Java 8 + Eclipse + Maven – Use Scala with Java 8

Scala+Java

Like every java developer you must be struggling with using Java and Scala in the same project. So, here is the solution with Eclipse, Maven and Java 8. Just follow these steps:

Step 1: Scala IDE Installation

  • Download Eclipse for JEE and create a workspace.
  • Install Scala IDE for eclipse by going into “Help” -> “Install New Software…”
  • Paste the following link into the “Work with:” field and press enter.
  • You need to check components which are checked in the image below only. (Click on the image to enlarge).

Scala IDE Installation Link: http://download.scala-ide.org/sdk/lithium/e44/scala211/stable/site

Eclipse Scala IDE Installation
Eclipse Scala IDE Installation

 

Step 2: Create a Maven Project

  • Right click in “Project Explorer” -> “New” -> “Other” .
  • In the filter field type “maven” and then select “Maven Project”.
  • Click next again twice now select Aechetype “maven-archetype-quickstart”  and click next.
  • Provide Group Id and Artifact Id and click finish.
  • Follow the image gallery below. (click to enlarge)

Step 3: Update pom.xml to add Maven Compiler Plugin and Scala Plugin & Repos

<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.harisaltaf</groupid>
	<artifactid>scala-with-java-maven-and-eclipse</artifactid>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>scala-with-java-maven-and-eclipse</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project .build.sourceEncoding>UTF-8</project>
		<java .version>1.8</java>
		<scala .majorVersion>2.11</scala>
		<scala .minorVersion>8</scala>
		<spark .version>1.6.1</spark>
		<spark .cassandra.connector.version>1.6.0</spark>
	</properties>

	<dependencies>
		<dependency>
			<groupid>junit</groupid>
			<artifactid>junit</artifactid>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupid>org.scala-lang</groupid>
			<artifactid>scala-library</artifactid>
			<version>${scala.majorVersion}.${scala.minorVersion}</version>
		</dependency>
		<dependency>
			<groupid>org.apache.spark</groupid>
			<artifactid>spark-core_${scala.majorVersion}</artifactid>
			<version>${spark.version}</version>
		</dependency>
		<dependency>
			<groupid>org.apache.spark</groupid>
			<artifactid>spark-sql_${scala.majorVersion}</artifactid>
			<version>${spark.version}</version>
		</dependency>
	</dependencies>

	<repositories>
		<repository>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
			<id>central</id>
			<name>Central Repository</name>
			<url>https://repo.maven.apache.org/maven2</url>
		</repository>
		<repository>
			<id>scala-tools.org</id>
			<name>Scala-tools Maven2 Repository</name>
			<url>http://scala-tools.org/repo-releases</url>
		</repository>
	</repositories>
	<pluginrepositories>
		<pluginrepository>
			<id>scala-tools.org</id>
			<name>Scala-tools Maven2 Repository</name>
			<url>http://scala-tools.org/repo-releases</url>
		</pluginrepository>
	</pluginrepositories>

	<build>
		<plugins>
			<plugin>
				<groupid>org.apache.maven.plugins</groupid>
				<artifactid>maven-compiler-plugin</artifactid>
				<configuration>
					<source />${java.version}
					<target>${java.version}</target>
				</configuration>
			</plugin>
			<plugin>
				<groupid>org.scala-tools</groupid>
				<artifactid>maven-scala-plugin</artifactid>
				<executions>
					<execution>
						<goals>
							<goal>compile</goal>
							<goal>testCompile</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
</project>

Notes:

  • Every setting is important in above pom.xml file.
  • Ignore the following maven error shown in the image below. Your project would build totally fine.
Eclipse Maven Scala Plugin Configuration Error
Eclipse Maven Scala Plugin Configuration Error to Ignore

Step 4: Update your project

  • By default your maven project is configured to work with JAVA 5 but we have updated our pom file to enforce maven to work with java 8.
  • Before update your project looks like below:

Eclipse Maven Project 1

  • To update your maven project right click on your project and go to “Maven” -> “Update Project…”.
  • In the dialogue box check “Force Update of Snapshot/Releases” like in the images below.
  • Now your project looks like following:

Eclipse Maven Project Update 3

Step 5: Add a Source Folder for Scala

  • Create a folder in your project as “src/main/scala”
  • Right click on your project go to “Properties” and in Properties select “Java Build Path” like following image (click to enlarge).

Eclipse Maven Scala Source Folder Configuration 1

Step 6: Add Scala Nature to Your Project

  • To Add Scala nature right click on your project go to “Configure” -> “Add Scala Nature” as the image below. This option would be visible if you have installed “Eclipse Scala IDE plugin” as mentioned in Step 1.

Eclipse Maven Scala Natutre 1

  • Now Your project would look like following:

Eclipse Maven Scala Natutre 2

Step 7: Add a new Scala Class and Call it from JAVA file

  • Add a new Scala file in Scala source folder like below
  • I have added a dummy Scala class file as ScalaClass.scala.
package com.harisaltaf

class ScalaClass {

  private val myName: String = "I am a Scala class";

  def getMyName(): String = {
    myName
  }

  def dummy = {
    println("I am a dummy method of ScalaClass")
  }
}
  • And I have called it from my JAVA class App.java.
package com.harisaltaf;

public class App {
	public static void main(String[] args) {
		
		System.out.println("Hello World!");
		
		ScalaClass sc = new ScalaClass();
		System.out.println(sc.getMyName());
		sc.dummy();
		
	}
}
  • Run your code from JAVA file because main method is there. You would get following output:

Hello World!
I am a Scala class
I am a dummy method of ScalaClass

Step 8: Debugging

Debugging is as easy as it was in java.

  • Add a break point in method dummy() of ScalaClass.
  • Now click the debug button from JAVA file because main method is there.

Complete Code:

Complete code is attached scala-with-java-maven-and-eclipse.