Call Scala code from Java?

interop, java, scala, scala-java-interop

Solution

There should be no problem in doing this.

- Have you verified (e.g. using WinZip or the `jar` utility) that your `.jar` file actually contains the relevant `.class` files? (use `jar tvf mylib.jar` to check)

- If you have verified that the correct `.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?

Problem

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.

Original source