Can command java
run a compiled scala code? If so, why do we have an exclusive command scala
?
5 Answers
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.

- 54,104
- 13
- 100
- 195
-
`java -cp ./scala-reflect-2.11.8.jar:./scala-library-2.11.8.jar HelloWorld` This did not work for me. – user1870400 Sep 07 '19 at 04:03
-
my hello world program is just this `object HelloWorld { def main(args: Array[String]): Unit = { println("Hello, world!") } }` – user1870400 Sep 07 '19 at 04:03
-
Thanks. What is "The scala command"? Is `scala` command the compiler? – Tim Mar 14 '20 at 23:15
-
@Tim `scala` is for running compiled code, the Scala compiler would be `scalac` – Landei Apr 01 '20 at 07:16
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.

- 257,207
- 101
- 511
- 656
-
1scala is an interpreter, while scalac is a compiler.. its not only for convenience but also a different technique. – atamanroman Sep 02 '10 at 10:10
-
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 Sep 02 '10 at 17:19
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
.

- 11,607
- 7
- 57
- 81
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
-
4Both 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 Sep 02 '10 at 17:17
If you look closely, the scala
command is simply a bash helper-script which summarize to the below command:
$cat /usr/local/Cellar/scala@2.11/2.11.12_1/libexec/bin/scala
execCommand \
"${JAVACMD:=java}" \
$JAVA_OPTS \
"${java_args[@]}" \
"${classpath_args[@]}" \
-Dscala.home="$SCALA_HOME" \
$OVERRIDE_USEJAVACP \
"$EMACS_OPT" \
$WINDOWS_OPT \
scala.tools.nsc.MainGenericRunner "$@"
There are 2 things required to run a .class
file compiled using scalac
( the scala compiler) using the java
command.
We need to include the
scala-library.jar
and thelocation of the .class file
in theclasspath
. To find the location ofscala-library.jar
, please execute the below:which scala /usr/local/opt/scala@2.11/bin/scala
In my case the
scala-*.jar
files are in :/usr/local/Cellar/scala@2.11/2.11.12_1/idea/lib
on MacThe location of the
Main2.class
file which is in/training/example1/scala
.
So, to execute the program we could use the below command:
java -cp /usr/local/Cellar/scala@2.11/2.11.12_1/idea/lib/scala-library.jar:/training/example1/scala/ Main2
EDIT-1: If you are using windows, please use semicolon(;) as the separator in java classpath command.

- 1,901
- 2
- 28
- 51