简体   繁体   English

将javax包导入python

[英]importing javax packages into python

I have (what I hope is) a very simple question. 我有(我希望是)一个非常简单的问题。

I would like to use some javax.crypto classes from within a python script, so be able to do something like: 我想从python脚本中使用一些javax.crypto类,因此能够执行以下操作:

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: 但是我不熟悉如何使python能够识别java包,目前,python当然只是说:

ImportError: No module named javax.crypto ImportError:没有名为javax.crypto的模块

Is it simply a case of adding some variable to $PYTHONPATH or is this just completely wrong? 这仅仅是在$ PYTHONPATH中添加一些变量的情况,还是完全错误?

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. Python和Java是独立的语言,而您正在使用的实现CPython具有自己的VM。 Use Jython if you want to bridge the two. 如果要桥接两者,请使用Jython

Under jython you use the syntax you describe. 在jython下,您使用描述的语法。 Basic types(strings, ints, floats) are converted automatically by jython when going from some .py code into java. 从一些.py代码转换为Java时,jython会自动转换基本类型(字符串,整数,浮点数)。 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
>>>

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

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