we have a project written in Java. It is a maven project, that has jsp pages and a lot of java code:
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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With