简体   繁体   中英

Exporting Scala project as jar from Eclipse

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

*1. At first I tried creating 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 this worked fine and dandy when launched from Eclipse, but when I export it as jar it'll give me 18 exceptions or so. I do now know how to replicate then "environment" in which Eclipse manages to launch this and I'm prety sure it relies on the fact that Scala is on my system already - I need a self contained jar with everything packed in there.

*2. My second try consisted of trying what lach suggested here How to 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 won't even run from Eclipse, although it gives only 6 or so exceptions starting with the famous NoClassDefFoundError . I have a feeling I'm not getting fully.qualified.ClassName right. *3. If the main Scala class is called "Dis.scala" and is located in package "pack" shouldn't this fully.qualified.ClassName be "pack.Dis"?

I'm using Jre 1.6 and Scala 2.9.2 EDIT: I have included all external imported jars, even scala-library.jar - everything is nice and packed in the jar

PS I am not familiar with Ant or Maven or Sbt. I just want my Scala project jared - if possible without getting into hairy things.

Here is what worked for me: 1. Create scala project 2. Create Wrapper java project 3. Add the scala-library.jar to you java project build path.

So you only need the 3rd step in addition since the rest looks similar to what I did. Then you can happily use: java - jar file.jar

EDIT: How to create a JAR File which contains Scala/Code which can be consumed by another Java Project, using Scala - Eclipse IDE.

  1. Create a new Scala Project and define an object with a main method as entry point.
  2. Now create a new Java Project and add your Scala Project to the new ones buildpath. Additionally add the scala-library.jar to the Java project.
  3. Now create a Wrapper class in the java project which calls your entry point class from the scala lib. Run the wrapper class to create a eclipse run configuration and test if you can call the scala project.
  4. Use the Export->Java->Runnable JAR file, Wizard now on the wrapper project.The eclipse run configuration will be used as entrypoint into the JAR. Depending on your needs you may want to : extract required libraries into generated JAR or Package required libraries into generated JAR

Finally you get a complete packaged JAR which you can use like this:

java - jar wrapped.jar

For me, it was relatively straightforward.

  1. Develop and test the project using the scala IDE (or eclipse for java).
  2. once ready, generate the jar for the project using file -> export method.

for submitting the spark (i was writing something for spark), i just had to mention --class option for specifying the main class for the jar.

hope to help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM