Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

run compiled Scala Files on Java Virtual Machine

Tags:

jvm

scala

Is it possible to run scala files with Java Virtual Machine? I am trying a lot but nothing works. Can someone give me some help with command line? Thanks a lot!

like image 748
Domenico Avatar asked Jun 05 '11 21:06

Domenico


People also ask

Can we run Scala on JVM?

Scala runs on the Java Virtual Machine Scala is compiled into Java Byte Code which is executed by the Java Virtual Machine (JVM). This means that Scala and Java have a common runtime platform. If you or your organization has standardized on Java, Scala is not going to be a total stranger in your architecture.

How do I run a Scala file in Java?

To run scala programs with the java command, you need to include the scala library as a runtime dependency of your application. I'm including the same Compile. jar from the java example as a compile time dependency, to show how everything behaves. The scala runtime is just a jar file (scala-library.

Does Scala code compile to Java?

Although Scala compiles to Java bytecode, it is designed to improve on many of the perceived shortcomings of the Java language.

How do I run a compiled code in Scala?

To run the code, type "scala HelloWorld". You can see that "Hello, world!" is printed on the screen. Let's understand the structure of the code. It consists of a method called "main".


2 Answers

Well, it depends on whether you are generating a JAR or class files, etc, but it is pretty simple: you run it like any Java program, but including the Scala library as a dependency.

java -cp .:/path/to/scala-library.jar MyApp
like image 166
Daniel C. Sobral Avatar answered Sep 28 '22 16:09

Daniel C. Sobral


Scala runs on the JVM. It does not have a separate virtual machine. But it does have its own libraries, so you will need to have Scala installed wherever you're running it.

If it's compiled you will have a .class file, so you just type in

scala -cp myClassPath myPackage.myFileName

as you would with Java. You don't need the -cp option if you've navigated to your classes folder.

It is possible to run Scala classes using the java command - you can probably Google how to do it, but you would need to sort out all the correct imports and there's no reason not to just use scala as above.

like image 26
Luigi Plinge Avatar answered Sep 28 '22 17:09

Luigi Plinge