如何将 Jython 文件导入 Groovy?
我想使用一些 Jython 代码作为 Groovy 闭包,因此我相信我需要将 Jython 文件导入到 Groovy 脚本中,并将其传递到 Groovy 的 MethodClosure() 函数中以生成闭包。
例如,我将其放入 Groovy 文件中:
import rand$py
cl = MethodClosure(rand$py.Rand(), "nextDouble")
显然 jythonc 已被弃用,并且我在系统上找不到它,因此我通过从另一个 .py 文件导入它并在另一个文件上运行 Jython 来生成 rand$py.class。 rand$py.class 与 Groovy 文件位于同一目录中。
这是当我尝试执行 Groovy 文件时遇到的错误:
Exception in thread "main" java.lang.NoClassDefFoundError: org/python/core/PyRunnable
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:634)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
........
at com.tinkerpop.gremlin.jsr223.ScriptExecutor.evaluate(ScriptExecutor.java:34)
at com.tinkerpop.gremlin.jsr223.ScriptExecutor.main(ScriptExecutor.java:20)
Caused by: java.lang.ClassNotFoundException: org.python.core.PyRunnable
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
看起来它正在找到它,因为不知何故它知道它是一个 Python 文件(或者它知道因为 $py 在它们的名称中?)。我通常不在 JVM 中工作,所以我可能错过了一些基本的东西。
编辑:我将 import rand$py
更改为 import bogus$py
以查看它是否生成相同的错误,但它没有生成,所以看起来它的某些部分正在查找类文件:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script1.groovy: 80: unable to resolve class bogus$py
@ line 80, column 1.
import bogus$py
^
1 error
I want to use some Jython code as a Groovy closure so I believe I need to import the Jython file into the Groovy script and pass it into Groovy's MethodClosure() function to generate the closure.
For example, I put this into a Groovy file:
import rand$py
cl = MethodClosure(rand$py.Rand(), "nextDouble")
Evidently jythonc is deprecated and I couldn't find it on my system so I generated the rand$py.class by importing it from another .py file and running Jython on the other file. The rand$py.class is located in the same directory as the Groovy file.
Here's the error I'm getting when I try to execute the Groovy file:
Exception in thread "main" java.lang.NoClassDefFoundError: org/python/core/PyRunnable
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:634)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
........
at com.tinkerpop.gremlin.jsr223.ScriptExecutor.evaluate(ScriptExecutor.java:34)
at com.tinkerpop.gremlin.jsr223.ScriptExecutor.main(ScriptExecutor.java:20)
Caused by: java.lang.ClassNotFoundException: org.python.core.PyRunnable
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
It looks like it's finding it because somehow it knows it's a Python file (or does it know that because $py is in them name?). I don't normally work in the JVM so I'm probably missing something fundamental.
EDIT: I changed import rand$py
to import bogus$py
to see if it generated the same error, and it did not so it looks like some part of it is finding the class file:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script1.groovy: 80: unable to resolve class bogus$py
@ line 80, column 1.
import bogus$py
^
1 error
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我猜想 Jython 库不在您的类路径上。查看 Groovy Running 上的类路径部分
I would guess that the Jython libraries are not on your classpath. Look at the classpath section on Groovy Running