Exporting a Scala project as a jar from Eclipse

I have a Scala project and I would like to export it as a jar.

* 1. First I tried to create a Java class for the project as an entry point

public class JMain { public static void main(String[] args) { System.out.println("Java main calling Scala main"); SMain.main(new String[] {""}); //SMain.main is the actual *main* 

and it worked fine and dandy when starting from Eclipse, but when I export it to jar it will give me 18 exceptions or so. Now I know how to replicate the "environment" in which Eclipse manages to run this, and I am sure that it relies on the fact that Scala is already in my system - I need a separate jar with everything that is packed there.

* 2. My second attempt was to try what lach suggested here How do I deploy a Scala project from Eclipse? namely:

 public class JMain { public static void main(String[] args) { System.out.println("Java Main"); List<String> argList = new ArrayList<String>(); argList.add("fully.qualified.ClassName"); //??? for (String s : args) argList.add(s); scala.tools.nsc.MainGenericRunner.main(argList.toArray(new String[0])); 

This time it will not even start from Eclipse, although it gives only 6 or so exceptions, starting with the famous NoClassDefFoundError . I have the feeling that I am not getting fully .qualified.ClassName correctly. * 3. If the main Scala class is called "Dis.scala" and is in the "pack" package, should it not be fully .qualified.ClassName "pack.Dis"?

I use Jre 1.6 and Scala 2.9.2 EDIT: I included all external imported jars, even scala -library.jar - everything is fine and packed in a jar

PS I am not familiar with Ant or Maven or Sbt. I just want the Scala jared project - if possible, not to get into hairy things.

+2
java scala compilation
source share
2 answers

Here's what worked for me: 1. Create a scala project 2. Create a Wrapper Java project 3. Add scala -library.jar to your java project build path.

So, you only need the third step, as the rest is similar to what I did. Then you can happily use: java - jar file.jar

EDIT: How to create a JAR file containing a Scala / code that can be used by another Java project using the scala - Eclipse IDE.

  • Create a new scala project and define the object with the main method as the entry point.
  • Now create a new Java project and add the scala project to the new buildpath. Also add scala -library.jar to your Java project.
  • Now create a Wrapper class in a java project that calls your entry point class from scala lib. Run the shell class to create an eclipse startup configuration and test if you can invoke the scala project.
  • Use the Export-> Java-> Runnable JAR file, now a wizard in a wrapper project. The eclipse launch configuration will be used as the entry point to the JAR. Depending on your needs, you can: extract the required libraries into the generated JAR or the Package of necessary libraries into the generated JAR

Finally, you will get a full batch JAR that you can use as follows:

java - jar wrapped.jar

+10
source share

It was relatively simple for me.

  • Design and test a project using the scala IDE (or eclipse for java).
  • after you are ready, create a jar for the project using the file β†’ export method.

to supply a spark (I wrote something for a spark), I just had to mention the -class option to specify the main class for the flag.

hope to help.

0
source share

All Articles