Create .jar from scala

I am developing an application on Eclipse using Scala and would like to create a .jar. I found tuto for this, but it uses the scala.tools.nsc package, and I don't know where I can find this thing.
I also tried to create a .class class, and then with the jar cmf .... command to generate .jar , but when starting .jar an error occurs. (NoClassFound)
I tried with sbt too, but when I compile my project that works with eclipse, a lot of errors appear.

Can someone explain to me how I can just create .jar with Eclipse or other tools.

+8
eclipse scala jar
source share
4 answers

Eclipse has a built-in option for creating executable jars, but it is well hidden. Also, it does not recognize the Scala main () signatures, so you have to create a Java class using the main () method.

  • Create a Java class using the main () method, which simply redirects the call to your Scala code.

  • Then right-click on the just created Java class and select: Run As β†’ Java Application. This will create an executable configuration that will later be used as part of your executable jar.

  • Now you are ready to dig up the runnable jar option from the Eclipse menu: File β†’ Export β†’ Java β†’ Runnable JAR file

  • In the presented dialog box, select the launch configuration created earlier (in step 2), name your bank (myapp.jar), select the dependency export options and click "Finish".

  • By default, a flag will be created in the Eclipse workspace.

  • Launch the jar using the line: Scala myapp.jar

Your question about missing images: Eclipse needs to be updated manually when adding or removing files. Right-click on your project and select Refresh.

This concludes the Eclipse intuitive tutorial.

Note. Instructions for Eclipse version 3.6.2.

+11
source share

Seasonings

When you create a jar from your classes, perhaps the dependencies are not included in this jar. When starting this jar, you need to place jars containing dependencies from the class path (with the -cp switch in java). The most important dependency is the jar of scala-library . Of course, knowing that NoClassDefFound was not found will help.

Sbt

When creating with sbt, maybe it doesn’t have any dependencies that you manually added to the Eclipse project? (Note: I did not use sbt).

Maven

I found that the clearest and most painless way is to go with maven only, or perhaps maven + Intellij Idea (free community edition) + Scala plugin. It works very smoothly.

For maven, you need to adapt the available Scala archetype a bit, because the libraries to which it refers are not the latest version, but other than that, it is very good.

Here is the pom.xml I am using: https://gist.github.com/1096870

Use the standard maven folder structure (the original root is src / main / scala), and then the mvn package creates jar fine.

+3
source share

Use the steps below for a while. But this is not a full-time solution. Go to build jb to build Spark-Scala. Workaround using the java class calling the main scala class. MainClass.java

 public class MainClass { public static void main(String[] args) { SampleApp app=new SampleApp(); app.main(args); } } 

SampleApp.scala

 class SampleApp { def main(args: Array[String]) { println("First Scala SampleApp")} } 

Export it as a jar using regular java jar export by choosing the main MainClass method. name the jar file as Sample.jar Run it in the cluster using the command below.

/ spark / bin / spark-submit --class com.scala.MainClass SampleScala.jar

You can get: First scala SampleApp

+1
source share
 % cat a.scala package foo object Whee { def main(args: Array[String]): Unit = { println("I'm in a jar") } } % scalac29 -d whee.jar a.scala % scala29 -cp whee.jar foo.Whee I'm in a jar % 
0
source share

All Articles