JPype - Issue importing & calling methods!
Here I'm attaching my code below
from jpype import *
from javax.swing import JFrame
classpath = "-Djava.class.path=praat.jar"
startJVM(getDefaultJVMPath(),"-ea",classpath)
frame = javax.swing.JFrame("Hello JPype")
label = javax.swing.JLabel("Hello JPype!", JLabel.CENTER)
frame.add(label)
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
frame.setSize(200, 100)
frame.show()
shutdownJVM()
When i run this program I get an error.
开发者_Python百科/Library/Python/2.6/site-packages/jpype/_pykeywords.py:18: DeprecationWarning: the sets module is deprecated
import sets
2010-02-01 22:26:27.473 Python[754:d07] Apple AWT Java VM was loaded on first thread -- can't start AWT.
Traceback (most recent call last):
File "swing.py", line 10, in <module>
frame = javax.swing.JFrame("Hello Jython")
File "/Library/Python/2.6/site-packages/jpype/_jpackage.py", line 53, in __call__
raise TypeError, "Package "+self.__name+" is not Callable"
TypeError: Package javax.swing.JFrame is not Callable
Is there any way to solve this problem. A normal Hello World program works fine, but when I'm trying to import packages I get similar issues.
Add the Java runtime library (rt.jar
) to the classpath and try again. The error indicates, that JFrame can't be found but it is inside rt.jar
.
If you import JFrame
into the local namespace, use it without the full namespace:
frame = JFrame("Hello Jython")
Same with JLabel, but remember to import it first.
To use the full namespace you need import javax.swing
instead of from javax.swing import JFrame
.
精彩评论