Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate .jar from scala

Tags:

eclipse

jar

scala

I'm developing an application on Eclipse with Scala and a would like to create a .jar. I have found tuto to do that, but it use the package scala.tools.nsc and I don't know where I can found this thing.
I have tried too, to generate the .class and then with the command jar cmf .... to generate the .jar but when I launch the .jar an error occur. (NoClassFound)
With sbt I have tried too, but when I compile my project that work with eclipse a lot of error appear.

Somebody can me explain how I can simply create a .jar with Eclipse or another tools.

like image 802
Snoopy Avatar asked Jul 21 '11 08:07

Snoopy


People also ask

How do I create a JAR file in scala?

Create a jar file using 'sbt assembly' command. If you run 'sbt assembly' command before adding plugin it shows errors. 2. Distribute all the JAR files necessary with a script that builds the classpath and executes the JAR file with the scala commands.

How do I create a JAR file in scala code in IntelliJ?

In the Create JAR from Modules window, select the folder icon in the Main Class text box. In the Select Main Class window, select the class that appears by default and then select OK. In the Create JAR from Modules window, ensure the extract to the target JAR option is selected, and then select OK.

Can Java JAR be used in scala?

To run scala programs with the java command, you need to include the scala library as a runtime dependency of your application. I'm including the same Compile. jar from the java example as a compile time dependency, to show how everything behaves. The scala runtime is just a jar file (scala-library.

How do I get scala code from jar?

Steps to get sources of a jar file as a zip :Drag and drop the jar for which you want the sources on the JAD. 3 JAD UI will open with all the package structure in a tree format. Click on File menu and select save jar sources. It will save the sources as a zip with the same name as the jar.


1 Answers

Eclipse has a build-in option to generate runnable jars, but it is well hidden. Also it does not recognize Scala's main() signatures, so you will have to create a Java class with a main() method.

  1. Create a Java class with a main() method which simply forwards the call to your Scala code.

  2. Then right click on your newly created Java class and select: Run As -> Java Application. This will create a runnable configuration which will later be used as a part of your runnable jar.

  3. Now you are ready to dig out the runnable jar option from the Eclipse's menu: File -> Export -> Java -> Runnable JAR file

  4. In the presented dialog select the Launch Configuration you have created earlier (in step2), name your jar (myapp.jar), select your dependency export options and click finish.

  5. The jar will be created in the Eclipse's workspace by default.

  6. Run the jar using the line: scala myapp.jar

Your question about missing images: Eclipse requires a manual refresh when files are added or removed. Right click on your project and select Refresh.

This concludes the tutorial on the highly intuitive Eclipse interface.

Note: Instructions for Eclipse version 3.6.2.

like image 158
Lex Avatar answered Sep 27 '22 22:09

Lex