views:

110

answers:

4

How can I create a jar from some Scala source code? I want to use some of that code in a Clojure project of mine.

Is there a simpler way than doing batch files as in this SO question?

Thanks, Alex

+5  A: 

Scala is in no way special when it comes to what you do with the .class files produced by its compiler. Just use the jar command with the c action flag.

However, you will need to have the scala-library.jar file in the class-path when you run the program that uses the Scala-compiled .class files. And be careful to use the scala-library.jar for / from the same Scala Development Kit that you used to compile the Scala source. As of yet, there isn't inter-version binary compatibility for Scala-generated .class files.

Randall Schulz
so if I have a .class file I am fine?
Alex Baranosky
Sure. And if you've got a directory (hierarchy) full of `.class` files from Scala, you can also just put its base name in the class-path. They really are not special from the perspective of other JVM code.
Randall Schulz
I'm having trouble getting my Intellij Clojure project to find the .class file I copied from my Scala project. I wonder if it is because I don't seem to be able to find ANY .class files in the Clojure project. Clojure seems to be doing some kind of magic.
Alex Baranosky
@Alex: Are you sure you properly respected the packages structure when copying `.class` files? Even though Scala doesn't care about directory / package correspondence (nor about file-name / class-name correspondence), the JVM insists that the directory hierarchy (of "loose" `.class` files or of JAR file contents) correspond precisely to the package structure of your `.class` files.
Randall Schulz
+2  A: 

One way in which creating .jar can be "simpler" is to use simple-build-tool, which supports the package* targets and requires rather little time to set up and use.

Radtoo
+1  A: 

You could use the maven scala plugin or the buildr tool.

Bozhidar Batsov
A: 

If you are using eclipse then just export your project as a jar file

goto file -> Export ->java -> as a jar file

sagar