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

Community
  • 1
  • 1
Alex Baranosky
  • 48,865
  • 44
  • 102
  • 150

4 Answers4

7

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
  • 26,420
  • 4
  • 61
  • 81
  • so if I have a .class file I am fine? – Alex Baranosky Aug 26 '10 at 00:53
  • 1
    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 Aug 26 '10 at 00:56
  • 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 Aug 26 '10 at 01:10
  • @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 Aug 26 '10 at 03:56
4

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
  • 291
  • 1
  • 5
1

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

Bozhidar Batsov
  • 55,802
  • 13
  • 100
  • 117
1

This has actually really helped

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

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

Just bit more explanation:

  1. Install the scala eclipse environment from typesafe
  2. Once you are done coding, export as a jar (file -> Export ->java -> as a jar file)
  3. from a command line you can run java -cp path_to_dependancies:your_newly_generated_jar:. your_main_object_name.

hth.

CruncherBigData
  • 1,112
  • 3
  • 14
  • 34
Sagar Varpe
  • 3,531
  • 4
  • 27
  • 43