Llew Vallis
Llew Vallis

Reputation: 337

How to package Scala libraries into runnable jar in Eclipse

Problem

I am having some trouble with creating a runnable jar file in Eclipse with Scala.

In Eclipse I have a Scala project that I wish to make into a runnable jar file. When I run the jar file it cannot locate the Scala standard library.

Details

IDE - I am using Scala IDE for extending Eclipse for Scala usage.

Setup - To export right click on my project and navigate down Export>Runnable Jar File. I select my run configuration and set the path to my desktop. I have the 'Extract' library handling option selected. My run configuration is a Java Application with my main class selected. My main class is written in Java whilst the rest of the project is written in Scala. I then run the create jar file through Windows CMD with java -jar myjarfile.jar.

Error - When I run the jar file a NoClassDefFoundError is thrown when the program first encounters a Scala standard library class (scala.util.Random in my case). The program runs my Scala files fine and even runs another project's files that I added as a project dependency under Eclipse. When I run the jar file with scala myjarfile.jar it cannot find the main class. Even if using scala did work it would not be acceptable because the user will likely not have anything related Scala installed.

I have researched similar questions on Stack Overflow and none of them have worked. So how do I access Scala standard libraries in my jar file? Packaging the Scala standard libraries into my jar sounds like a good option but I have not idea how to do this.

Upvotes: 1

Views: 1036

Answers (2)

Llew Vallis
Llew Vallis

Reputation: 337

Use a Wrapper Project

Create a pure Java project and delegate the main method to your core Scala project. Add the Scala standard library to the Java project (found here) and export it. It should work perfectly.

Upvotes: 0

chengpohi
chengpohi

Reputation: 14227

There are 2 options for package a runnable jar for Scala with SBT:

  1. sbt-assembly package your class files as a fat jar, it will also package the Scala standard library.
  2. SBT Native Packager it can package class files to a runnable packages include:zip, tgz, RPM, DEP, Docker.

Upvotes: 1

Related Questions