Domanda

Sto cercando di ottenere un semplice esempio di utilizzo di jpype (versione: 0.5.4.6) Per istanziare una classe Java personalizzata da Python. esso Segue liberamente un esempio: http://hustleplay.wordpress.com/2010/02/18/jpype -tutorial / ed è relativo a: JPype Mal di testa

Ecco il codice Python (in /User/me/jpiepet/testjpype.py):

from jpype import *
cpopt="-Djava.class.path=%s" % ("/Users/me/jpypeTest")
startJVM(getDefaultJVMPath(),"-ea",cpopt)
print "JVM path:",getDefaultJVMPath()
print "classpath:",cpopt
java.lang.System.out.println("Hello World!!")
testPkg = JPackage('pkg')
Test = testPkg.Test
Test.speak("hi")
shutdownJVM()
.

Ed ecco il codice Java (in /User/me/jpypetest/pkg/test.java):

package pkg;
public class Test {
    private String msg;

    public Test() {
        msg = "nothing so far...";
    }

    public static void speak(String msg) {
        System.out.println(msg);
    }
}
.

compilato usando:

javac Test.java
.

per produrre /User /me/jpypetest/pkg/test.class senza errori (Ho anche provato a mettere il test.class nella directory di lavoro)

Esecuzione del codice Python dà:

> python testjpype.py 
JVM path: /System/Library/Frameworks/JavaVM.framework/JavaVM
classpath: -Djava.class.path=/Users/me/jpypeTest
Hello World!!
Traceback (most recent call last):
  File "testjpype.py", line 9, in <module>
    Test.speak("hi")
  File "/Users/me/dev/lib/python2.7/site-packages/jpype/_jpackage.py", line 53, in __call__
    raise TypeError, "Package "+self.__name+" is not Callable"
TypeError: Package pkg.Test.speak is not Callable
.

System è un Mac che esegue Mavericks con:

> java -version
java version "1.7.0_45"
Java(TM) SE Runtime Environment (build 1.7.0_45-b18)
Java HotSpot(TM) 64-Bit Server VM (build 24.45-b08, mixed mode)

> javac -version
javac 1.7.0_45

> python --version
Python 2.7.5

> python -c 'import sys;print("%x" % sys.maxsize, sys.maxsize > 2**32)'
('7fffffffffffffff', True)
.

Un metodo JPype per l'elenco delle classi Java importate sarebbe anche molto utile

Molte grazie per aver guardato questo!

È stato utile?

Soluzione

Il problema sembra che l'accesso a un attributo di una classe JPackage restituisca una classe JPackage quando ciò che vuoi veramente è una JClass.Cambiare il testjpype.py per essere questo funzionato per me:

from jpype import *
cpopt="-Djava.class.path=%s" % ("/Users/me/jpypeTest")
startJVM(getDefaultJVMPath(),"-ea",cpopt)
java.lang.System.out.println("Hello World!!")
Test = JClass('pkg.Test')
Test.speak("hi")
shutdownJVM()
.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top