I'm using Netbeans to write Scala and Java.
Netbeans generated a .jar file for the Scala project. I tried importing that file in the Java project. But I couldn't import any class from that .jar file into my Java project.
I also tried importing scala-library.jar to the java project, and could import classes from that jar.
I want to write my library in Scala, then expose a small interface that only involves Java stuff, then write a Java wrapper so that people can use it as a Java package. Is that possible? How do I do it?
Thank you very much.
Yes, Scala classes can be called from Java and vice versa.
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.
Calling methods on Scala instances work exactly as in Java.ScalaClass scalaClass = new ScalaClass(true); assertEquals(2, scalaClass. plus(1, 1)); Note that Scala classes cannot have static methods. The convention is to put methods of static nature on companion objects.
A Scala trait with no method implementation is just an interface at the bytecode level and can thus be used in Java code like any other interface.
There should be no problem in doing this.
jar
utility) that your .jar
file actually contains the relevant .class
files? (use jar tvf mylib.jar
to check).class
files exist in your jar
file, what is the runtime error you are seeing? Is it a NoClassDefFoundError
? If so, which class
cannot be found? How are you referring to the (Scala) class in your (Java) code?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