python包安装——jcc安装

本文详细介绍了Python包JCC的用途及安装步骤。JCC用于生成C++代码,使得C++和Python可以调用Java代码。安装过程中,首先需要配置Java JDK的环境变量,然后下载并修改JCC的setup.py文件,最后通过命令行进行安装。文章还讨论了JCC的使用,包括如何生成C++和Python的包装器、类路径考虑、使用distutils与setuptools的区别、分发egg等,并提供了JCC运行时API的功能概述。
摘要由CSDN通过智能技术生成

什么是JCC?

JCC 是 Python 的一个包,主要用来生成 C++ 代码以在 C++ 和 Python 程序中调用 Java 代码。


JCC安装步骤

第一步:首先要安装java的jdk,关于java的jdk安装请自行百度。

1、安装完JDK后配置环境变量  计算机→属性→高级系统设置→高级→环境变量

2、系统变量→新建 JAVA_HOME 变量 。

变量值填写jdk的安装目录(例如 E:\Java\jdk1.7.0)

3、下面开始“classpath”的配置。选中“系统变量”查看是否有classpath项目,如果没有就点击“新建”,如果已经存在就选中classpath选项,点击“编辑”按钮,然后在“变量名”中填写“classpath”,在“变量值”中填写“C:\Program Files\Java\jdk1.6.0\jre\lib”(根据安装路径填写)。

4、现在可以进行“path”的配置了。同上在“classpath”设定时类似,“变量名”输入框填写“path”,“变量值”输入框填写“C:\Program Files\Java\jdk1.6.0\bin”(根据安装路径填写)。



第二步:先到主页下载Jcc安装包https://pypi.python.org/pypi/JCC/2.21,下载后要修改setup.py文件中JDK这个变量的值,在我的程序中JDK这个变量的值如下所示:

JDK = {
    'darwin': JAVAHOME or JAVAFRAMEWORKS,
    'ipod': '/usr/include/gcc',
    'linux2': '/usr/lib/jvm/java-7-openjdk-amd64',
    'sunos5': '/usr/jdk/instances/jdk1.6.0',
    'win32': JAVAHOME,
    'mingw32': JAVAHOME,
    'freebsd7': '/usr/local/diablo-jdk1.6.0'
}

于是直接把JAVAHOME换成‘C:/Program Files/Java/jdk1.7.0_67’


第三步:到setup.py文件所在的目录运行如下指令

python setup.py build
python setup.py install


ok


第四步:运行 import jcc出错!!!

c:\Python27\python.exe: DLL loadfailed: The specified module could not be found

.; 'jcc' is a package and cannotbe directly executed

解决方案:

把JVM.dll所在的目录加入系统变量path中

我的在系统变量path中加入C:\Program Files\Java\jre7\bin\server

also try

>import ctypes

>ctypes.windll.LoadLibrary("jvm.dll")

if that fails,typically a popup-window shows an error message and complains about somemissing DLL - I had this with msvcr71.dll

you may need anddownload/copy that dll and install in your lib path (e.g.  C:\windows)



现在import jcc 成功,证明安装基本成功。


JCC使用说明(有空再来翻译)

Generating C++ and Python wrappers with JCC

JCC started as a C++ code generator for hiding the gory details of accessing methods and fields on Java classes via Java's Native Invocation Interface. These C++ wrappers make it possible to access a Java object as if it was a regular C++ object very much like GCJ's CNI interface.

It then became apparent that JCC could also generate the C++ wrappers for making these classes available to Python. Every class that gets thus wrapped becomes a CPython type.

JCC generates wrappers for all public classes that are requested by name on the command line or via the --jar command line argument. It generates wrapper methods for all public methods and fields on these classes whose return type and parameter types are found in one of the following ways:

  • the type is one of the requested classes

  • the type is one of the requested classes' superclass or implemented interfaces

  • the type is available from one of the packages listed via the --package command line argument

Overloaded methods are supported and are selected at runtime on the basis of the type and number of arguments passed in.

JCC does not generate wrappers for methods or fields which don't satisfy these requirements. Thus, JCC can avoid generating code for runaway transitive closures of type dependencies.

JCC generates property accessors for a property called field when it finds Java methods namedsetField(value)getField() or isField().

The C++ wrappers are declared in a C++ namespace structure that mirrors the Java classes' Java packages. The Python types are declared in a flat namespace at the top level of the resulting Python extension module.

JCC's command-line arguments are best illustrated via the PyLucene example:


$ python -m jcc           # run JCC to wrap
    --jar lucene.jar      # all public classes in the lucene jar file
    --jar analyzers.jar   # and the lucene analyzers contrib package
    --jar snowball.jar    # and the snowball contrib package
    --jar highlighter.jar # and the highlighter contrib package
    --jar regex.jar       # and the regex search contrib package
    --jar queries.jar     # and the queries contrib package
    --jar extensions.jar  # and the Python extensions package
    --package java.lang   # including all dependencies found in the 
                          # java.lang package
    --package java.util   # and the java.util package
    --package java.io     # and the java.io package
      java.lang.System    # and to explicitely wrap java.lang.System
      java.lang.Runtime   # as well as java.lang.Runtime
      java.lang.Boolean   # and java.lang.Boolean
      java.lang.Byte      # and java.lang.Byte
      java.lang.Character # and java.lang.Character
      java.lang.Integer   # and java.lang.Integer
      java.lang.Short     # and java.lang.Short
      java.lang.Long      # and java.lang.Long
      java.lang.Double    # and java.lang.Double
      java.lang.Float     # and java.lang.Float
      java.text.SimpleDateFormat
                          # and java.text.SimpleDateFormat
      java.io.StringReader
                          # and java.io.StringReader
      java.io.InputStreamReader
                          # and java.io.InputStreamReader
      java.io.FileInputStream
                          # and java.io.FileInputStream
      java.util.Arrays    # and java.util.Arrays
    --exclude org.apache.lucene.queryParser.Token
                          # while explicitely not wrapping
                          # org.apache.lucene.queryParser.Token
    --exclude org.apache.lucene.queryParser.TokenMgrError
                          # nor org.apache.lucene.queryParser.TokenMgrError
    --exclude org.apache.lucene.queryParser.ParseException
                          # nor.apache.lucene.queryParser.ParseException
    --python lucene       # generating Python wrappers into a module
                          # called lucene
    --version 2.4.0       # giving the Python extension egg version 2.4.0
    --mapping org.apache.lucene.document.Document 
              'get:(Ljava/lang/String;)Ljava/lang/String;' 
                          # asking for a Python mapping protocol wrapper
                          # for get access on the Document class by
                          # calling its get method
    --mapping java.util.Properties 
            
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值