score:50
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.
score:2
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.
score:4
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
score:6
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
.
score:14
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.
Source: stackoverflow.com
Related Query
- Can java run a compiled scala code?
- Can scala code compiled with JDK 7 run on JVM 8?
- Can a Scala program be compiled to run on any JVM, without having Scala installed on the given machine?
- Can we write Scala code inside a Java program?
- Can I compile a Scala project with mixed java and scala code with dependencies both ways in Maven?
- run compiled Scala Files on Java Virtual Machine
- Can Scala run on its own (without installing Java first) and does it matter which Java is installed?
- How to run Scala compiled code in command line?
- Can I run JUnit 4 to test Scala code from the command line?
- How can I use the Scala REPL to test java code - java and scala REPL giving different outputs
- How is Scala faster than Java when both are compiled into `.class` file which is run by JVM only?
- Can I generate the interim java code in a scala project
- how to change the config file for correct java location to run scala ide - 'returned exit code 1'
- How to compile and run scala code at runtime from a java program?
- Scala REPL: Can I run a piece of code at every start?
- Run scala app with maven dependencies plus some java code
- What are All the ways we can run a scala code in Apache-Spark?
- how can i run my scala code coverage fully
- Samples of Scala and Java code where Scala code looks simpler/has fewer lines?
- How can I convert a Java Iterable to a Scala Iterable?
- What can I do to my scala code so it will compile faster?
- How are Scala traits compiled into Java bytecode?
- Automated Java to Scala source code conversion?
- How can I convert Scala Map to Java Map with scala.Float to java.Float k/v conversion
- Automatically convert Scala code to Java code
- How to run scala code on Intellij Idea 11?
- How can I tell what Scala version a .class file was compiled with?
- How to use Scala varargs from Java code
- reader writer state monad - how to run this scala code
- Can I use a scala class which implements a java interface from Java?
More Query from same tag
- Consolidating a data table in Scala
- Deep flatten Scala Maps (or uJson Objs) - how to?
- Maven with Jacoco and Sonar 0% coverage
- How does Scala's Vector work?
- What is the canonical way to create objects from rows of a Spark dataframe?
- Scala Macros: Checking if a Java Field is marked as transient
- How to deal with so-called "fluent" or "builder" interfaces
- What are all the instances of syntactic sugar in Scala?
- How to detect the heap size from within a Scala program?
- Scala: Ending Stream.iterate
- Fold a Shapeless HList into Future of that same HList, flattening Future elements found, if any
- how to write custom linear collection in scala
- scala.io.Source.fromInputStream does not throw the MalformedInputException further
- Intellij Fails to Import SBT project
- Parsing options that take more than one value with scopt in scala
- Questions on Java and Scala and parallelization
- Get elements of type structure of row by name in SPARK SCALA
- Dealing with multiple options and logging not found scenarios
- Not able to connect to Cloud SQL using Java SocketFactory Library
- Scala and Writing map functions
- Scala Compiler Plugin Rewrite Function Definition As A Tuple: error: not found: value scala.Tuple2
- @BeanProperty with PropertyChangeListener support?
- In scala, is it possible to initialise a singleton object from a TypeTag?
- column Name of LongType instead of Utf8Type in cassandra cli column family
- scala delimited continuations typing
- How do I convert a java.util.Map to scala.collection.immutable.Map in Java?
- Comparing floating point numbers with tolerance fails assertion
- Checking whether a number is prime in Scala
- How to add a new column to data frame based on two columns of other data frames
- Scala functional programming dry run