简体   繁体   English

PythonInterpreter导入Java类

[英]PythonInterpreter importing java classes

I have the following file structure: 我具有以下文件结构:

src
|___mod
|   |__ __init__.py
|   |__ pycode.py
|   |__ javacode.java
|
|___main
    |__ start.java

contents of pycode.py: pycode.py的内容:

from mod import javacode as jv
...

Inside start.java , I try to run pycode.py with the python interpreter. 里面start.java ,我尝试运行pycode.py与Python解释器。

PythonInterpreter py = new PythonInterpreter();
py.exec("from mod.pycode import *");

However, I get the following error: 但是,出现以下错误:

ImportError: cannot import name javacode

It's confusing because it seems to be able to find the package, but for some reason fails to find the file. 这令人困惑,因为它似乎能够找到该程序包,但是由于某种原因未能找到该文件。 I have infact verified that it found the package because it complains if you supply the wrong package name. 我实际上已验证它找到了该程序包,因为如果您提供了错误的程序包名称,它会抱怨。

To give more information, I am running the program on windows in eclipse. 为了提供更多信息,我正在Eclipse的Windows上运行该程序。 I am using the pydev plugin for eclipse. 我正在使用pydev插件进行日食。 I have added the bin folder of the project as a source folder for pydev (as suggested by one source), and I have the following at the start of my program: 我已将项目的bin文件夹添加为pydev的源文件夹(如一个源所建议),并且在程序开始时具有以下内容:

static {
    PythonInterpreter.initialize(System.getProperties(), PySystemState.getBaseProperties(),
            null);
}

Can anyone give me an idea on how I can fix this? 谁能给我一个解决方法的想法?

The reason why it was not working is because I placed javacode.java in a python package. 它不起作用的原因是因为我将javacode.java放在了python包中。

According to the book Jython Essentials , doing this will mark the java file as a python module: 根据Jython Essentials的书,执行此操作会将java文件标记为python模块:

Jython also allows access to Java classes and packages through the import statements. Jython还允许通过import语句访问Java类和包。 Jython is able to load classes through the underlying Java Virtual Machine (JVM), both from the Java classpath and from the directories in sys.path. Jython能够通过底层Java虚拟机(JVM)从Java类路径和sys.path目录中加载类。 Conceptually, you can think that for the purpose of loading Java classes, the directories in sys.path have been appended to the classpath. 从概念上讲,您可以认为出于装载Java类的目的,sys.path中的目录已附加到类路径中。 This means there is no need to mark Java packages on sys.path with __init__.py modules, because that would make them Python packages. 这意味着无需使用__init__.py模块在sys.path上标记Java软件包,因为这会使它们成为Python软件包。

So after this, I organized the files as such: 因此,在此之后,我将文件组织如下:

src
|___pymodules
|   |__ __init__.py
|   |__ pycode.py
|
|___mod
|   |__ javacode.java
|
|___main
    |__ start.java

Inside start.java : start.java内部:

PythonInterpreter py = new PythonInterpreter();
py.exec("from pymodules.pycode import *");

The program now executes perfectly both in eclipse and even after making it into a standalone jar 现在,该程序即使在eclipse中执行,甚至在将其放入独立的jar中之后,都可以完美执行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM