简体   繁体   English

如何使用Eclipse使用Python导入Java类?

[英]how to import Java class with Python using Eclipse?

I'm trying to write Jython where the Python file imports classes from Java I'm using Eclipse with PyDev. 我正在尝试编写Jython,其中Python文件从Java导入类我使用Eclipse和PyDev。

My Python code looks like: 我的Python代码如下:

from eclipsejavatest import eclipseJavaTest
from eclipsejavatest import JavaClass

class eclipsePyPrint(eclipseJavaTest):
    def eclipsepyMain(self):
        print "python main method"
        eclipseJavaTest.printerCount(4)
        print eclipseJavaTest.gotoPython()
        eclipseJavaTest.printerSentence()

        samplepyClass = JavaClass("Jython plain")
        samplepyClass.setName("jython fancy")
        print samplepyClass.getName()

but I'm getting the error ImportError: No module named eclipsejavatest 但我收到错误ImportError: No module named eclipsejavatest

The Python project references the Java project. Python项目引用Java项目。 I've tried exporting the Java project and adding the .jar to the Jython Class Path for the Python project. 我已经尝试导出Java项目并将.jar添加到Python项目的Jython类路径中。

I'm not sure what to do to get this to work. 我不知道该怎么办才能让它发挥作用。

I am also using Eclipse with Pydev. 我也在使用Eclipse和Pydev。 I wanted to call Java classes from a Java project in my workspace from a program inside a Jython project folder. 我想从Jython项目文件夹中的程序调用工作区中的Java项目中的Java类。 I did NOT have to right click on the java package and set it as a pydev project. 我没有必要右键单击java包并将其设置为pydev项目。 In my setup the java source folder would not show up when I tried to add it to the PYTHONPATH regardless of whether I configured it as a Pydev folder. 在我的设置中,当我尝试将它添加到PYTHONPATH时,无论我是否将其配置为Pydev文件夹,都不会显示java源文件夹。 I had to add it as an external library. 我不得不将它添加为外部库。

I right-clicked on the Jython project, selected properties, selected PyDev - PYTHONPATH, selected the External Libraries tab, and then selected the add source folder button. 我右键单击Jython项目,选中属性,选择PyDev - PYTHONPATH,选择External Libraries选项卡,然后选择add source folder按钮。 I added the folder with the .class files from the java project; 我在java项目中添加了带有.class文件的文件夹; C:\\blah blah blah\\workspace\\javafoldername\\bin. C:\\ blah blah blah \\ workspace \\ javafoldername \\ bin。 At first I tried to use ...\\javafoldername\\src to add the .java files but that didn't work. 起初我尝试使用... \\ javafoldername \\ src来添加.java文件,但这不起作用。

想出来 - 我必须右键单击java包并将其设置为pydev项目然后我必须进入python项目并添加.class文件

试试:

import eclipsejavatest.eclipseJavaTest as eclipseJavaTest

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

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