tags:

views:

235

answers:

4

Can command java run a compiled scala code? If so, why do we have an exclusive command scala?

+4  A: 

Scala is designed to integrate easily with applications that run on modern virtual machines, primarily the Java virtual machine (JVM). The main Scala compiler, scalac, generates Java class files that can be run on the JVM. -> http://www.artima.com/scalazine/articles/steps.html

As long as you have installed the scala runtime you should be fine: compile classes with scalac and run them with java.

atamanroman
+17  A: 

You can run byte code generated by Scala if you include all necessary runtime libs for Scala (scala-library.jar, scala-swing.jar ...) in the classpath. The scala command does this automatically, and supports Scala specific command line arguments.

Landei
+3  A: 

Yes, it can. Scala is compiled down to Java bytecode. But remember that it depends on the Scala runtime classes, so you need to still have Scala's jar files on the classpath.

If so, why do we have an exclusive command scala?

Convenience wrapper.

Thilo
scala is an interpreter, while scalac is a compiler.. its not only for convenience but also a different technique.
atamanroman
You could start the scala interpreter with the "java" command if you tried...
Thilo
You could start the compiler, too--it's just another class that needs to be run (scala.tools.nsc.Main, with appropriate arguments). But convenience is convenient, so why do it the hard way?
Rex Kerr
A: 

Just want to add my own answer as additional value for the future readers:

scala, if run without parameter, will run an interactive shell

scala, if run with a text file name as parameter, will regard the file as a scala script

those two can't be done using java

nanda
Both of those can be done, but it is particularly annoying to do it--you have to call scala.tools.nsc.MainGenericRunner and make sure the appropriate tools are in the classpath. Why not just let the scala script do it for you?
Rex Kerr