什么是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
现在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