0

I'm having an issue with Scala and Java interoperability which Google and SO seem to be unhelpful (I've seen similar questions, but none offered a working solution for my case).

I have created a jar file in Java (hosted here, if you need it to answer this question) which contains a class with a static method. However, I can't seem to access this static method from Scala. Here's the code:

val graph1 = ...
val graph2 = ...
val union = DirectedGraph.merge(graph1, graph2)

The method exists, and I can access it with normal Java code. In fact, the following works:

DirectedGraph<OWLClass> graph1 = ...;
DirectedGraph<OWLClass> graph2 = ...;
DirectedGraph<OWLClass> union = DirectedGraph.merge(graph1, graph2);

I've checked that the jar files being used by java and scala are the same. And I also checked to see if the method was indeed there with javap.

Is there an idea out there to understand and possibly solve this problem?

7
  • It throws two errors: error while loading DirectedGraph, class file 'fetchers.jar(pt/remoteowl/objects/DirectedGraph.class)' is broken (class scala.MatchError/class DirectedGraph (of class scala.reflect.internal.Symbols$ClassSymbol)) and value merge is not a member of object pt.remoteowl.objects.DirectedGraph Commented Dec 19, 2013 at 18:37
  • It would be helpful to know what exact Scala version are you using, since “class file X is broken” have had various causes in different versions. Commented Dec 19, 2013 at 18:54
  • Scala version 2.10.1 (OpenJDK 64-Bit Server VM, Java 1.7.0_25) Commented Dec 19, 2013 at 18:55
  • 1
    The jar you provide is not self-contained; if I try to use it in the Scala repl I get "warning: Class pt.remoteowl.jar.Dependency not found - continuing with a stub". Are you sure that's not the root cause? I can't try to reproduce this on my own machine unless you provide all of the needed dependencies. Commented Dec 19, 2013 at 18:56
  • 1
    The problem was indeed that a .class file was missing from the jar. Thank you! Commented Dec 20, 2013 at 7:57

1 Answer 1

2

The most likely reason (in my experience) is that the Java compiler treats annotations as optional, so that if one of your dependencies uses an annotation and there is no dependency which contains this annotation, it compiles without problems. However, the Scala compiler considers this an error. You may want look at DirectedGraph source along with its supertypes.

Sign up to request clarification or add additional context in comments.

2 Comments

This explains way Java doesn't complain about the jar but Scala does. Thank you
Here is a related question and answer stackoverflow.com/a/18600851/1148030

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.