2013-02-14 8 views
13

Hinweis: Ich fand die Lösung und antwortete mir. Allerdings habe ich keine Ahnung, warum diese Einstellung falsch war und das Problem verursacht hat. Ich bin immer noch daran interessiert, eine gute Erklärung darüber zu haben, wie das Jython-Import-System funktioniert. wenn jemand sich darum kümmert, das Kopfgeld zu bekommen, dann antworte bitte darauf.Kann Java-Klassen von Jython-Modul nicht importieren


Ich arbeite an einem vorhandenen Java EE-Projekt, wo ich Berechnungen in Python durchführen muss. Ich bin in den ersten Phasen der Integrationstests, aber ich habe bereits ein Problem. Ich lese Chapter 10 of Jython book aber kann immer noch keine Lösung finden. Ich lese auch Chapter 8 (Module und Pakete für Code-Wiederverwendung), aber für mich ist es unklar.

Eine Erklärung, wie das Jython-Import-System funktioniert und wie es konfiguriert wird, wäre sehr willkommen.

Das Problem:

$ jython -v 
import: 'exceptions' as org.python.core.exceptions in builtin modules 
Jython 2.5.3 (2.5:c56500f08d34+, Aug 13 2012, 14:54:35) 
[Java HotSpot(TM) Server VM (Oracle Corporation)] on java1.7.0_10 
import: import site # precompiled from /home/me/jython/2.5.3/Lib/site$py.class 
import: 'sys' as sys in builtin modules 
import: import os # precompiled from /home/me/jython/2.5.3/Lib/os$py.class 
import: 'errno' as org.python.modules.errno in builtin modules 
import: 'posix' as org.python.modules.posix.PosixModule in builtin modules 
import: import posixpath # precompiled from /home/me/jython/2.5.3/Lib/posixpath$py.class 
import: import stat # precompiled from /home/me/jython/2.5.3/Lib/stat$py.class 
import: 'java' as java package 
import: 'File' as java class 
import: 'IOException' as java class 
import: 'org' as java package 
import: 'Py' as java class 
Type "help", "copyright", "credits" or "license" for more information. 

>>> import pendulum.generator.BuildingType 
import: import pendulum # precompiled from /path/to/project/build/classes/pendulum/__init__$py.class 
import: import pendulum.generator # precompiled from /path/to/project/build/classes/pendulum/generator/__init__$py.class 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
ImportError: No module named BuildingType 

Meine Frage ist: Was mache ich falsch und wie behebe ich die Dinge BuildingType Schnittstelle erfolgreich zu importieren? Vielleicht steht das Problem in CLASSPATH, aber ich habe keine Ahnung, welcher Wert angemessen sein könnte.


-Code Organisation:

$ tree build/classes/pendulum/generator/ src/pendulum/generator/ 
build/classes/pendulum/generator/ 
├── BuildingType.class 
├── __init__.py 
└── __init__$py.class 

src/pendulum/generator/ 
├── BuildingType.java 
└── __init__.py 

Pfad Import definiert durch eine private Jython registry file:

$ cat ~/.jython 
python.path=\ 
/path/to/project/build/classes:\ 
/path/to/project/src:\ 
/home/me/jdevel/extras/2.5.3/Lib:\ 
/home/me/jdevel/extras/2.5.3/Lib/site-packages 

Ich bin sicher, Jython abholt die Pfade, weil ich, dass geprüft mit sys.path bei Jython Eingabeaufforderung.

BuildingType.java

package pendulum.generator; 

public interface BuildingType { 
    public String getBuildingName(); 
    public String getBuildingAddress(); 
    public String getBuildingId(); 
} 
+0

versuchen "von Pendel.generator import BuildingType " – sarwar

+0

ImportError: Name kann nicht importiert werden BuildingType – Paolo

+1

Ist jython.jar in jre/lib/ext? –

Antwort

4

Nach viel Zeitverschwendung in Versuch/Fang Ansatz konnte ich die Antwort selbst finden.

.jython haben wie folgt aussehen:

python.path=\ 
/path/to/project/build:\ 
/path/to/project/src:\ 
/home/me/jdevel/extras/2.5.3/Lib:\ 
/home/me/jdevel/extras/2.5.3/Lib/site-packages 

Nicht dies:

python.path=\ 
/path/to/project/build/classes:\ 
/path/to/project/src:\ 
/home/me/jdevel/extras/2.5.3/Lib:\ 
/home/me/jdevel/extras/2.5.3/Lib/site-packages 

Insbesondere /path/to/project/build/classes auf den Importpfad hinzuzufügen, ist falsch (auch wenn es das spiegelt Dateisystemhierarchie), während /path/to/project/build richtig ist und das Problem gelöst hat.

4

Sie müssen als python.path auch CLASSPATH setzen.

Mit der gleichen Verzeichnisstruktur, das funktioniert für mich:

jython10$ CLASSPATH=build/classes/ jython -v Building.py 
import: 'exceptions' as org.python.core.exceptions in builtin modules 
import: import site # precompiled from /usr/local/Java/jython2.5.3/Lib/site$py.class 
import: 'sys' as sys in builtin modules 
import: import os # precompiled from /usr/local/Java/jython2.5.3/Lib/os$py.class 
import: 'errno' as org.python.modules.errno in builtin modules 
import: 'posix' as org.python.modules.posix.PosixModule in builtin modules 
import: import posixpath # precompiled from /usr/local/Java/jython2.5.3/Lib/posixpath$py.class 
import: import stat # precompiled from /usr/local/Java/jython2.5.3/Lib/stat$py.class 
import: java package as '/Users/sdm7g/jaxp/jython10/build/classes/pendulum' 
import: 'pendulum' as java package 
import: 'pendulum' as java package 
import: java package as '/Users/sdm7g/jaxp/jython10/build/classes/pendulum/generator' 
import: 'pendulum.generator' as java package 
import: 'BuildingType' as java class 

Referenz: Working with CLASSPATH (Jython Book).

+0

Vielen Dank. Ohne den Classpath zu setzen, konnte ich meine Klassen nicht über die Kommandozeile verwenden ('java.lang.ClassNotFoundException'). Aus Eclipse ging alles gut. – Paolo