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.
views:
536answers:
4Yes. If you include the appropriate scala library jar file in the classpath, a scala program can be run using java, since compiled scala code is the same as compiled java code.
google "compile scala java jvm", first hit: Compiling Scala for the Java Virtual Machine.
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 :
- 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.
- 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).
- It can be found in your Scala distribution: lib/scala-library.jar
If you ever used any Java library, just treat Scala like them. Get Scala's jar (the library one), and either pack it together with your jar using one of the programs available for that, or distribute it together.