简体   繁体   中英

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.

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM