开发者

importing javax packages into python

I have (what I hope is) a very simp开发者_开发问答le question.

I would like to use some javax.crypto classes from within a python script, so be able to do something like:

from javax.crypto import Cipher

cipher = Cipher.getInstance('AES/CTR/NoPadding')

But I am not familiar with how to do this get python to be able to recognise java packages, at the moment python, of course, simply says:

ImportError: No module named javax.crypto

Is it simply a case of adding some variable to $PYTHONPATH or is this just completely wrong?

Many Thanks, Chris


It's very completely wrong. Python and Java are separate languages, and CPython, the implementation you're using, has its own VM. Use Jython if you want to bridge the two.


Under jython you use the syntax you describe. Basic types(strings, ints, floats) are converted automatically by jython when going from some .py code into java. If you want to be processing your own objects you have to start writing interface wrappers.

C:\>SET PATH=C:\jython2.5.2\bin;%PATH%

C:\>jython
Jython 2.5.2 (Release_2_5_2:7206, Mar 2 2011, 23:12:06)
[Java HotSpot(TM) Client VM (Sun Microsystems Inc.)] on java1.6.0_24
Type "help", "copyright", "credits" or "license" for more information.
>>> from javax.crypto import Cipher
>>> cipher = Cipher.getInstance('AES/CTR/NoPadding')
>>> cipher
javax.crypto.Cipher@1296d1d
>>>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜