Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 549
Vegard Avatar asked Dec 12 '09 15:12

Vegard


2 Answers

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
like image 51
Petr Gladkikh Avatar answered Oct 22 '22 09:10

Petr Gladkikh


Yes. 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.

like image 23
Sean Reilly Avatar answered Oct 22 '22 09:10

Sean Reilly