Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala and Java in same project

Can I use Scala and Java in the same project? I am new with programming so it's a litte bit confusing for me.

From my research I have read that the best combination to get a good project is the combination Java/Spring or Scala/Lift. Why do these combinations exist? Is it not possible to use Scala with Spring or Java with Lift?

In case I use Scala, should I have the whole configuration in Scala code? Or can I have external resources so I shouldn't modify every time the Scala code.

like image 395
Ayoub Abid Avatar asked Mar 01 '16 10:03

Ayoub Abid


People also ask

Can we mix Java and Scala?

Is it possible to mix Scala and Java code? Yes, there is the ability to mix both types of code. It is possible to create an SBT project, put Scala code in src/main/scala and java code in src/main/java in the same project and make it work.

Can we use Java code in Scala?

Seamless Interop: Scala can use any Java library out of the box; including the Java standard library! And pretty much any Java program will work the same in Scala, just by converting the syntax. A Scalable Language: the name Scala comes from Scalable Language.

Can Java use Scala libraries?

Scala is designed to work smoothly with Java and Java libraries like JMSL. This is clearly evident in how Scala picks up the same CLASSPATH environment variable Java uses, so if the Java library you wish to use is already there in the CLASSPATH, then you're good to go.

Can we run Scala on JVM?

As already mentioned, Scala code compiled on JDK 8 should run without problems in later JVMs.


2 Answers

Q. Can I use Scala and Java in the same project?

Sure, you can. You can use most of the JVM based languages together in the same project.

Q. How do I go about it?

The basic idea is to configure your build tool to compile both the Java and the Scala code. For example, if you use Maven, you can use the Maven Scala plugin for compiling Scala code. There are two options for using this plugin:

  1. Disable Maven Compiler plugin that compiles Java code by default. Then, let the Maven Scala plugin compile both the Java and Scala code.
  2. Continue using the Maven Compiler plugin to compile Java. Configure the Maven Scala plugin to compile only Scala code.

I prefer option 2 since I have found Scala compilation to be quite slow (although the performance has been improving steadily with each new Scala version) so using the Maven Compiler plugin reduces the overall build time.

Q. Is it not possible to use Scala with Spring or Java with Lift?

Sure you can use Scala with Spring and Java with Lift. You can check my sample application that uses Java, Scala and Spring.

You will notice that the sample application has separate source folders for Java and Scala code, although that is not mandatory and can be configured in the Maven configuration.

Q. In case I use Scala, should I have the whole configuration in Scala code?

I am assuming you mean Spring configuration. It is entirely up to you whether you wish to configure Spring using XML files, Java classes or Scala classes. All approaches work. It depends on which style you are more comfortable with and prefer.

You can even spread the configuration across multiple classes or even a combination of XML and classes.

like image 94
manish Avatar answered Sep 30 '22 20:09

manish


Short answer

Yes

Longer answer

Scala and Java work good together.

Scala main compilation target can be compiled is Java bytecode that runs on any JVM. On top of runtime compatibility, the Scala language itself supports interoperability.

So the basic project setup looks like

./src
   /main
       /scala
       /java
    test
        /scala
        /java

How everything is bound together is determined by the build system that you are using, i.e. sbt, gradle or maven all provides good support here.

like image 33
Andreas Neumann Avatar answered Sep 30 '22 19:09

Andreas Neumann