Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integrating Scala into an existing project in Java

Tags:

we have a project written in Java. It is a maven project, that has jsp pages and a lot of java code:

  • servlets for handling user requests and dealing out responses.
  • classes of logic used by servlets in order to process the required logic.
  • classes of SQL generators used to connect to database and perform the specific queries required by the logic.

Well, we are looking towards Scala. For example, we use mappers in order to get a collection of entities from the database and after that transform this collection, filter it and return to the servlet. That seems like a good thing to do in Java. The question is - how can I integrate that into an existing project? Can I use in my logic files written in Java some Scala classes that again use the mapper files written in Java?

like image 603
SPIRiT_1984 Avatar asked Dec 05 '13 09:12

SPIRiT_1984


People also ask

Can Java and Scala work together?

Scala is compiled to Java bytecodes, and you can use tools like javap (Java class file disassembler) to disassemble bytecodes generated by the Scala compiler. In most cases, Scala features are translated to Java features so that Scala can easily integrate with Java.

Can we use Scala code in Java?

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 JVM run Scala?

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.


1 Answers

I work on a mixed Java/Scala project right now (previously a Java-only project). Remember, there is no difference as far as the JVM is concerned between a .class file generated from javac vs. one generated from scalac.

For example: I implement Java interfaces and extend Java classes in Scala with no problems. I also instantiate Java beans and 'Scala' spring beans together in the same definition file (with scala beans having dependencies on java beans and vice-versa), and I have both Java and Scala beans coexisting together in a spring integration messaging pipeline.

The two languages inter-operate quite well, though where collections are concerned, you'll probably want to take a look at Scala's JavaConversions and JavaConverters objects.

For building, we use Ant (I'd personally prefer SBT for a new project, but if you're dealing with a pre-existing java project that uses ant, it's likely to be more trouble than it's worth to change build systems). To configure ant to compile scala files, have a look at this question.

like image 130
James Adam Avatar answered Oct 23 '22 18:10

James Adam