scalajvm

Can a Scala program be compiled to run on any JVM, without having Scala installed on the given machine?


If I've writen a Scala program, can I compile it in a way so that anybody with a standard Sun Java JVM can run it? I guess the Scala compiler would have to include the Scala-specific API code in the compiled project? The reason I'm interested is that our class projects can usually be delivered in whatever language one prefers, but TAs grading the deliveries usually want to run the code either on their own machine or on lab machines.


Solution

  • You do not need to do anything special to run your compiled Scala program on JVM. It is just plain JVM bytecode. Only thing you need is to make sure that standard Scala library (scala-library.jar) is included in your class path at runtime. This is only extra dependency (or perhaps you also may need scala-swing.jar if you use Swing wrappers for your GUI).

    If you worry about convenience for the user you may even package your application to single jar so it contains contents of scala-library.jar and your own classes and resources. But personally I'd do that only if this jar is executable (can be run as java -jar yourApplication.jar).

    Notes about Scala library :

    1. It provides just that - library. No interpreter, no compiler or special execution environment of some sort; so you should not worry about class loading issues.
    2. It should match version of Scala you are using during compilation your program (Scala library for 2.7 and for 2.8 are not interchangeable).
    3. It can be found in your Scala distribution: lib/scala-library.jar