为什么 Jython 拒绝找到我的 Java 包?

发布于 2024-08-10 08:34:40 字数 689 浏览 3 评论 0原文

我知道这很愚蠢,但由于某种原因 Jython 拒绝查找 javax.swing。我正在使用 Java 1.6.0_11。这是我的启动脚本:

@echo off

"%JAVA_HOME%\bin\java" -Xmx1024M -classpath ".;c:\Projects\Jython2.5.1\jython.jar" org.python.util.jython 

我的输出如下所示:

Jython 2.5.1 (Release_2_5_1:6813, Sep 26 2009, 13:47:54)
[Java HotSpot(TM) Client VM (Sun Microsystems Inc.)] on java1.6.0_10
Type "help", "copyright", "credits" or "license" for more information.
>>> import javax.swing
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named swing
>>> import javax
>>> dir(javax)
['__name__']
>>>

I know it's something silly, but for some reason Jython refuses to find javax.swing. I'm using Java 1.6.0_11. This is my start-up script:

@echo off

"%JAVA_HOME%\bin\java" -Xmx1024M -classpath ".;c:\Projects\Jython2.5.1\jython.jar" org.python.util.jython 

My output looks like:

Jython 2.5.1 (Release_2_5_1:6813, Sep 26 2009, 13:47:54)
[Java HotSpot(TM) Client VM (Sun Microsystems Inc.)] on java1.6.0_10
Type "help", "copyright", "credits" or "license" for more information.
>>> import javax.swing
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named swing
>>> import javax
>>> dir(javax)
['__name__']
>>>

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

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

发布评论

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

评论(3

愁以何悠 2024-08-17 08:34:40

Jython 很可能没有扫描您的包裹。启动时,Jython 尝试遍历其路径上的 jar 和类文件并扫描 Java 包。这是必要的,因为没有办法通过反射来查找 Java 包。可以故意关闭包扫描,或者您可能缺乏写入权限,想要将缓存的信息写出,请参阅 http ://wiki.python.org/jython/PackageScanning 了解更多信息。导入 Java 类的最佳方法是逐类显式地执行此操作,如下所示:

from javax.swing import JFrame

即使包扫描关闭或无法工作,此方法也应该始终有效,并且是推荐的方法(尽管它可能有点乏味) )。如果您确实想要导入包(或者如果您想要执行“from javax.swing import *”,这也取决于包扫描 - 但不鼓励),您将需要找出包扫描不起作用的原因。

Most likely Jython is not scanning your packages. On startup, Jython tries to go through the jars and class files on its path and scan for Java packages. This is necessary because there is no way to look for Java packages by reflection. Package scanning can be deliberately turned off, or you could lack write privileges where it wants to write the cached information out see http://wiki.python.org/jython/PackageScanning for more. The best way to import Java classes is to do so explicitly class by class, like so:

from javax.swing import JFrame

This method should always work, even if package scanning is off or otherwise unable to work, and is the recommended approach (though it can be a bit tedious). If you do want to import packages (or if you want to do "from javax.swing import *" which also depends on package scanning - but is discouraged) you will need to figure out why your package scanning isn't working.

终遇你 2024-08-17 08:34:40

我遇到了类似的问题,事实证明由于独立的 Jython dist 不支持缓存,因此它也不支持“导入*”方法。官方 Jython 文档中没有明确记录这一点,但我根据许多不同的错误报告得出了这一结论:

从最后一个链接值得注意:

正如 Oti 指出的,在独立模式下,您必须进行完全导入才能成功。

要解决您的问题,请使用通过使用“标准”选项安装 jython 生成的非独立标准 jython.jar

如果您想将 jython.jar 与您的应用程序一起打包和分发,以防用户没有安装 Jython,那么您还需要将完整的“Lib”文件夹从 jython 安装目录复制/粘贴到您最终所在的位置放置 jython.jar。这允许访问未包含在标准 jar 文件中的 python stdlib。

更新
经过更多尝试后,我认为即使在使用独立 jar 时,我也可以修复启用“import *”类型导入的问题。需要做的就是启用缓存!

您可以通过在运行 jython 时向 jvm 添加以下选项来完成此操作:

-Dpython.cachedir.skip=false -Dpython.cachedir=所需的缓存路径

(请注意,第二个参数是可选的,如果留空,将使用默认值)

如果您遇到问题运行嵌入在应用程序中的 InteractiveConsole(这就是我的问题),您可以在初始化控制台之前添加这些属性:

    Properties props = new Properties();
    props.put("python.cachedir.skip", "false");
    props.put("python.cachedir", "DESIRED CACHE PATH"); // again, this option is optional
    InteractiveConsole.initialize(System.getProperties(), props, new String[0]);

I had similar issues, and it turns out that since the standalone Jython dist does not support caching, it also does not support the "import *" approach. This is not clearly documented anywhere in the official Jython docs, but I concluded this based on a number of different bug reports:

Notable from that last link:

So as Oti noted, in standalone you must do full imports to succeed.

To fix your issue, use the non-standalone standard jython.jar generated by installing jython using the 'Standard' option.

If you wanted to package and distribute jython.jar with your application, in case a user does not have Jython installed, then you will also need to copy/pase the complete "Lib" folder from the jython installation directory into whichever location you end up placing jython.jar. This enables access to the python stdlib which is not included in the standard jar file.

UPDATE:
After playing around more, I think I have a fix to enable "import *" type imports even when using the standalone jar. All that needs to be done is to enable caching!

You can do this by either adding the following options to the jvm when running jython:

-Dpython.cachedir.skip=false -Dpython.cachedir=DESIRED CACHE PATH

(Note that the second argument is optional, and if left blank, a default value will be used)

If you are having an issue running the InteractiveConsole embedded in an app (which is what my problem was) you can add these properties before initializing the console:

    Properties props = new Properties();
    props.put("python.cachedir.skip", "false");
    props.put("python.cachedir", "DESIRED CACHE PATH"); // again, this option is optional
    InteractiveConsole.initialize(System.getProperties(), props, new String[0]);
岁月染过的梦 2024-08-17 08:34:40

我使用的是 Java 1.6.0_11

不,您正在使用

java1.6.0_10 上的[Java HotSpot(TM) 客户端 VM (Sun Microsystems Inc.)]

如果从 Jython 分发目录中删除 cachedir,然后重试,会发生什么情况?

另外,为什么你要这样明确地设置类路径?为什么不简单一点呢

java -jar jython.jar

I'm using Java 1.6.0_11

No, you're using

[Java HotSpot(TM) Client VM (Sun Microsystems Inc.)] on java1.6.0_10

What happens if you delete the cachedir from the Jython distribution directory, and try again?

Also, why are you explicitly setting the classpath that way? Why not simply

java -jar jython.jar

?

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