无法从Scala访问Java静态方法 [英] Cannot access a Java static method from Scala

查看:76
本文介绍了无法从Scala访问Java静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Scala和Java互操作性方面遇到了一个问题,谷歌和SO似乎无济于事(我见过类似的问题,但没有一个可以为我的案例提供有效的解决方案).

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).

我已经用Java创建了一个jar文件(托管在此处,如果您需要它来回答这个问题),其中包含带有静态方法的类.但是,我似乎无法从Scala访问此静态方法.这是代码:

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)

该方法存在,我可以使用常规Java代码访问它.实际上,以下起作用

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);

我检查了java和scala使用的jar文件是否相同.我还检查了javap是否确实存在该方法.

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?

推荐答案

(根据我的经验)最可能的原因是Java编译器将注释视为可选的,因此,如果您的某个依赖项使用了注释,则不会包含此注释的依赖项,它可以毫无问题地进行编译.但是,Scala编译器认为这是一个错误.您可能想看看DirectedGraph源及其超类型.

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.

这篇关于无法从Scala访问Java静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆