问题
Naive attempts fail: e.g.
from mypackage import SomeClass$
gives
javax.script.ScriptException: SyntaxError: no viable alternative at character '$' in at line number 1
This question is unrelated to java nested classes, which are easily importable in Jython.
Such a class is for example the scala.Predef$
class, which one can then access with Predef$.foobar
.
How can I achieve this import?
回答1:
I found a solution using Java Reflection like this:
from java.lang import Class
person = Class.forName("full.path.to.Person$").getField("MODULE$").get(None)
It's not pretty but it works. It might be nicer to use the Scala reflection API if that's available.
来源:https://stackoverflow.com/questions/44623470/how-do-i-import-a-java-classname-with-a-dollar-sign-or-refer-to-a-field-or