jython 中的类导入

发布于 2024-08-05 02:19:12 字数 435 浏览 0 评论 0原文

我有一个 jython 脚本,需要包含一个类(在本例中来自 JUnit)。我在“some/path/junit.jar”中有 junit jar。我的脚本是:

from junit.textui import TestRunner

TestRunner.Main(["name of some class here"])

我像这样运行它:

java -cp "some/path/junit.jar" -jar jython.jar script.py

但它抱怨:

    from junit.textui import TestRunner
ImportError: No module named junit

我怎样才能让它看到/导入正确的类?

I've got a jython script that needs to include a class (from JUnit in this case). I've got the junit jar in "some/path/junit.jar". My script is:

from junit.textui import TestRunner

TestRunner.Main(["name of some class here"])

I'm running it like this:

java -cp "some/path/junit.jar" -jar jython.jar script.py

but it complains that:

    from junit.textui import TestRunner
ImportError: No module named junit

How can I make it see/import the correct class?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

内心荒芜 2024-08-12 02:19:12

当您使用 -jar 选项时,java 会忽略类路径。像这样直接运行jython类,

java -cp "some/path/junit.jar:some/other/path/jython.jar" org.python.util.jython script.py

你必须喜欢他们的命名约定(所有小写的类名)。我假设类名称为 Jython,我尝试了几次才弄清楚这一点。

When you use -jar option, java ignores classpath. Just run jython class directly like this,

java -cp "some/path/junit.jar:some/other/path/jython.jar" org.python.util.jython script.py

You have to love their naming convention (all lower-case class name). I assumed the class name would be Jython and it took me a few tries to figure this out.

送你一个梦 2024-08-12 02:19:12

作为 ZZ Coder 答案的一个可能更简单的替代方案,您还可以在 Jython 的启动脚本上使用 -J-cp 参数:(

    jython -J-cp "some/path/junit.jar" script.py

我会将其作为注释附加到前一个答案中,但我的声誉还不允许这样做。 )

As a - maybe simpler - alternative to ZZ Coder's answer, you can also use the -J-cp parameter on Jython's start script:

    jython -J-cp "some/path/junit.jar" script.py

(I would have appended this as a comment to the former answer, but my reputation does not allow it yet.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文