JAVA + SCALA in a Single IntelliJ Project

Mix JAVA and SCALA in a Single IntelliJ Project Module

Kefei Mo
Analytics Vidhya
5 min readMay 14, 2021

--

image created by author

So, you might wonder whether it is possible to combine JAVA and SCALA in the same IntelliJ project.

The answer is yes, I have Apache Spark project in my IntelliJ. It uses standard maven directory that has java and scala directories under src/main of module directory.

Many posts online are claiming the same sort of strange issues with mixing in the same project. You might be one of those people that feel real uneasy approaching scala if this has remained unresolved for years with no real articles explaining why.

In this post, I will guide you how to achieve this in IntelliJ (Community Edition, Version: 2021.1.1)

Without further ado, let’s walk through the process.

Tutorial

  1. Create a new default POM project
  • File New ProjectMaven (use the default Maven project, don’t create one from archetype) → Next
  • Give the project a Name Finish

The original pom.xml will look something like this. Leave it as what it is for now.

2. Create a simple java file

Right click on the source directory “java” → new Java Class

In my case, I first created a Package directory (java new Package), then a Java Class file

Write some simple code and run. (Check this link if you are new to IntelliJ and not sure how to run the application)

The result.

Next, let’s integrate some scala code.

3. Enable scala Framework Support

Note that there is NO option for us to create a Scala class or object under New.

What we need to do is to add scala framework support.

Right click on the module name (mixJavaScalaPOM for my case) → Add Framework Support…

In the left panel check the Scala option → select Scala version in Use library (if not existed yet, click on Create and select Scala SDK jar) → OK

Right click on java source path again, and you can see there is a Scala Class option now

4. Refine the file structure

Even though we are able to create Scala Class now, it is confusing to put Scala file under java source file path. Let’s create a source file path for Scala.

4.1 create a new directory called “scala” under main

(Note the color is grey, indicating a normal directory; the source path directory is in blue color in Intellij convention.)

4.2 assign scala directory as source path

File Project Structure → (on the left of the prompt) Project SettingsModules

Right click on the newly created scala directory → choose SourcesOK

(Notice scala directory turned blue after this operation)

Now create a Hello-world scala object file and test if the project works.

Yay. 😀 Work like a charm.

One last step if we want to package the code.

5. Configure the pom file for dependencies and plugins.

  • The JVM (e.g., JDK-8) by default doesn’t include scala compiler, so we need to include the scala dependency (i.e., scala-library) if want to package the code and run as a jar file on JVM. There are other dependencies to include based on the project.
  • We also include some plugins (maven-compiler-plugin, maven-shade-plugin, and scala-maven-plugin) so we can make uber jar.

Here is the updated pom.xml (It is a bit wordy though)

Now we can package the code with mvn package command. (I prefer to package my maven project on command line. But check this link if you prefer to achieve the same goal but inside IntelliJ.)

Here we have two jar files.

  • a uber-jar with the dependencies (mixJavaScalaPOM-1.0-SNAPSHOT.jar)
  • a normal-jar (original-mixJavaScalaPOM-1.0-SNAPSHOT.jar)

Now test the package with java -cp command.

(Notice the original jar (non-uber jar) didn’t work for the scala class because it didn’t package the scala library dependencies.)

Summary

In this tutorial, we walk though the process to create a mixed JAVA + SCALA project in IntelliJ. The project can be download from Github. Please leave a comment if you have any questions. See you next time.

--

--

Kefei Mo
Analytics Vidhya

ML engineer / data story teller / electrical engineer / digital illustrator / 3D modeler. I am writing data science blog with my cousin.