Cannot access a Java static method from Scala

允我心安 提交于 2019-12-25 02:28:37

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/20689122/cannot-access-a-java-static-method-from-scala

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!