user3335040
user3335040

Reputation: 669

What is the best way to run a Java program that also has Scala code?

Say you have a team of 3 where 2 members are working in Java and the other member is working in Scala. Is it possible to compile and run this program with the Java compiler / JVM or do you need to use the Scala compiler / JVM?

What is the best way to run a program that has both Java and Scala source code? Any further elaboration on this topic would be helpful.

Upvotes: 1

Views: 152

Answers (4)

Jus12
Jus12

Reputation: 18024

Most IDEs allow having both Java and Scala code together. Netbeans and IDEA have this out of the box. Possibly eclipse too.

If you want to work on Scala and Java code separately, compile the Scala code to a JAR and ask the Java guys to use it as an external library. To the JVM Scala and Java code are indistinguishable.

The Java guys will need the above jar plus scala-library.jar available in the Scala distribution in SCALA_HOME/lib. If the Scala code uses actors etc, then it will need additional Scala jars available in the above dir.

I have been doing this for years. The Java guys sometimes don't even know its written in Scala.

Upvotes: 1

Ashalynd
Ashalynd

Reputation: 12563

Scala is JVM-based language, so Scala compiler is able to understand Java programs, but not vice versa. So if the program has both Scala and Java files, compiling it using Scala compiler seems to be the only option.

Regarding how these programs should be run, you can assembly the result into a JAR, which can be run by a JVM in a "usual" way. If you use sbt for your program, it has a handy 'assembly' plugin that will do most of the job for you.

Upvotes: 2

tkroman
tkroman

Reputation: 4798

To compile Scala code you will need Scala compiler, which will produce classfiles that are JVM-compliant. So the answer to the first par of the question is yes. And yes, you can run compiled Scala code in JVM, but since it uses Scala library, you need to provide it in your classpath.

Upvotes: 0

Erik Kaplun
Erik Kaplun

Reputation: 38207

The canonical way would be simply by using SBT instead of Maven. You can also keep using Maven though: http://davidb.github.io/scala-maven-plugin/

See also the discussion at http://www.scala-lang.org/old/node/5248.html

Upvotes: 0

Related Questions