怎样在linux中安装nginxjpype

python调用Java-JPype使用介绍(一)(转)
转自:/developerworks/cn/opensource/os-cn-jpype/index.html
JPype 是一个能够让 python 代码方便地调用 Java 代码的工具,从而克服了 python
在某些领域(如服务器端编程)中的不足。
JPype 可以从 sourceforge 网站上下载:&目前
JPype 最新的版本为 0.5.4,支持 python 2.5 和 2.6. 本文以 Windows XP 平台,python
2.5.4 为例阐述。
安装 JPype 前需要先安装 python 。从&&下载
python 并安装,安装路径选择 C:\Python25\,安装完成后在本地 C 盘应有 C:\Python25 目录,该目录下有
python.exe 文件。 Python 安装完后,双击下载的 JPype 安装文件即可安装 JPype 。
JPype 遵循的许可证是 Apache License V2.0 。
JPype 的使用
一个简单的 hello world 程序
下面是一个简单的 python 程序,通过 JPype 调用 Java 的打印函数,打印出字符串。
清单 1. hello world
import jpype
jvmPath = jpype.getDefaultJVMPath()
jpype.startJVM(jvmPath)
jpype.java.lang.System.out.println( “ hello world! ” )
jpype.shutdownJVM()
JPype 提供的 startJVM() 函数的作用是启动 JAVA 虚拟机,所以在后续的任何 JAVA
代码被调用前,必须先调用此方法启动 JAVA 虚拟机。
jpype.startJVM() 的定义
startJVM(jvm, *args)
jpype.startJVM() 的参数
参数 1: jvm, 描述你系统中 jvm.dll 文件所在的路径,如“ C:\Program
Files\IBM\Java50\jre\bin\j9vm\jvm.dll ”。可以通过调用
jpype.getDefaultJVMPath() 得到默认的 JVM 路径。
参数 2: args, 为可选参数,会被 JPype 直接传递给 JVM 作为 Java 虚拟机的启动参数。此处适合所有合法的 JVM
启动参数,例如:
-agentlib:libname[=options]
-classpath classpath
清单 2. jpype.startJVM() 的用法示例
import jpype
jvmPath = jpype.getDefaultJVMPath()
jvmArg = “ -Xint ”
jpype.startJVM(jvmPath,jvmArg)
判断 JVM 是否启动
JPype 提供的 jpype.isJVMStarted() 函数的作用是判断 JVM 是否已启动。
jpype.isJVMStarted() 的定义&
isJVMStarted()
jpype.isJVMStarted() 的返回值
返回值为 True 表示 JVM 已经启动,返回值为 False 表示 JVM 还未启动
清单 3. jpype.isJVMStarted() 的用法示例
import jpype
jvmPath = jpype.getDefaultJVMPath()
jvmArg = “ -Xint ”
if not jpype.isJVMStarted():
jpype.startJVM(jvmPath,jvmArg)
当使用完 JVM 后,可以通过 jpype.shutdownJVM() 来关闭 JVM,该函数没有输入参数。当 python
程序退出时,JVM 会自动关闭。
引用第三方的 Java 扩展包
很多时候,在 python 项目中需要调用第三方的 Java 扩展包,这也是 JPype 的一个重要用途。为了使编程者方便地在
python 代码中调用已有的 Java 扩展包,我们可以再在 JVM 启动参数增加:
-Djava.class.path=ext_classpath
以下为调用第三方 Java 扩展包示例,(假设第三方 Java 扩展包的所在路径是 E:\JavaExt)
清单 4. 调用第三方 Java 扩展包示例
import jpype
jvmPath = jpype.getDefaultJVMPath()
ext_classpath = “ E:\\JavaExt ”
jvmArg = “ -Djava.class.path = ” + ext_classpath
if not jpype.isJVMStarted():
jpype.startJVM(jvmPath, jvmArg)
访问 Java 的系统属性
有时,某些 Java 应用需要设置或者获取 JVM 中的系统属性。
在 JVM 启动时设置系统变量示例:
在 JVM 的启动参数中加入如下参数:
-Dproperty=value&
假设你要设置的属性名为 yourProperty,属性值为 yourValue 。
清单 5. JVM 启动时设置系统变量示例
import jpype
jvmPath = jpype.getDefaultJVMPath()
jvmArg = “ -DyourProperty=yourValue ”
if not jpype.isJVMStarted():
jpype.startJVM(jvmPath,jvmArg)
清单 6. 在程序中设置系统变量示例
import jpype
prop = “ yourProperty ”
value = “ yourValue ”
system = jpype.JClass('java.lang.System')
system.setProperty(str(prop),str(value))
清单 7. 在程序中获取系统变量示例
import jpype
prop = “ yourProperty ”
system = jpype.JClass('java.lang.System')
value = system.getProperty(str(prop))
Java 类型到 python 类型的转换
表 1. Java 类型到 python 类型的转换
转换成的 python 类型
byte, short and int
float and double
int of value 1 or 0
unicode of length 1
other Java object
JavaObject
array Class
JavaArrayClass
使用 Java 对象
清单 8. Java 类定义
public class JavaClass {
public String value = "";
public JavaClass(String value) {
this.value =
public String getValue() {
return this.
public void setValue(String val) {
this.value =
对于上述的 Java 类,以下的代码介绍了如何在 Python 中构造并且使用相应的对象。 首先是获得对应 Java 类:
清单 9. 获得对应 Java 类
import jpype
javaClass = jpype.JClass('.ibm.javaproject.JavaClass')
调用 Java 类的构造函数生成实例:
清单 10. 调用 Java 类的构造函数生成实例
value = “ oldvalue ”
javaInstance = javaClass(value)
调用 Java 方法:
清单 11. 调用 Java 方法
print javaInstance.getValue()
javaInstance.setValue( “ newvalue ” )
print javaInstance.getValue()
运行结果:
清单 12. Python 代码的运行结果
Java exception 错误处理
异常处理是程序编写者们必须考虑的问题,在使用 JPype 的过程中,所有的 Java exception 将被自动转换成
jpype.JavaException 。 以下是 Jpype 处理 Java exception 的示例:
清单 13. 处理 Java exception
from jpype import JavaException
#Code that throws a java.lang.RuntimeException
except JavaException, ex:
if JavaException.javaClass() is java.lang.RuntimeException:
print "Caught the runtime exception : ", JavaException.message()
print JavaException.stackTrace()
应用实例(Password Cipher)
下面我们用一个简单的应用实例来说明如何在 python 代码中调用 Java 类。
Java 类定义
假设我们已用 Java 语言编写了一个类:PasswordCipher,该类的功能是对字符串进行加密和解密。它提供了一个对外的接口
run() 函数,定义如下 :
清单 14. PasswordCipher 定义
public class PasswordCipher {
public static String run(String action, String para){
run 函数接收两个参数,第一个参数代表所要进行的操作,传入“ encrypt ”表示对第二个参数 para
做加密操作,返回加密结果。如果第一个参数为“ decrypt ”则返回对 para 的解密操作的结果。在 run
函数中有可能会有异常抛出。
Python 代码
我们先将 PasswordCipher.class 存放在目录“ F:\test\cipher ”下,然后用 python
语言编写下面的代码:
清单 15. Python 代码
import jpype
from jpype import JavaException
jvmPath = jpype.getDefaultJVMPath()
#the path of jvm.dll
classpath = "F:\\test\\cipher"
#the path of PasswordCipher.class
jvmArg = "-Djava.class.path=" + classpath
if not jpype.isJVMStarted():
#test whether the JVM is started
jpype.startJVM(jvmPath,jvmArg)
#start JVM
javaClass = jpype.JClass("PasswordCipher")
#create the Java class
testString = "congratulations"
print "original string is : ", testString
#call the run function of Java class
encryptedString = javaClass.run("encrypt", testString)
print "encrypted string is : ", encryptedString
#call the run function of Java class
decryptedString = javaClass.run("decrypt", encryptedString)
print "decryped string is : ", decryptedString
except JavaException, ex:
print "Caught exception : ", JavaException.message()
print JavaException.stackTrace()
print "Unknown Error"
jpype.shutdownJVM()
#shut down JVM
运行该程序后得到的结果:
清单 16. 该程序运行的结果是:
original string is :
congratulations
encrypted string is :
B0CL+niNYwJLgx/9tisAcQ==
decryped string is :
congratulations
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。Error 404 (Not Found)!!1
404. That’s an error.
The requested URL /492384/ was not found on this server.
That’s all we know.使用命令pip install jpype1可安装jpype。
如果出现如下情况:
creating build\lib.win-amd64-3.5\jpypex
copying jpypex\__init__.py -& build\lib.win-amd64-3.5\jpypex
creating build\lib.win-amd64-3.5\jpypex\swing
copying jpypex\swing\AbstractAction.py -& build\lib.win-amd64-3.5\jpypex\swing
copying jpypex\swing\pyutils.py -& build\lib.win-amd64-3.5\jpypex\swing
copying jpypex\swing\__init__.py -& build\lib.win-amd64-3.5\jpypex\swing
running build_ext
building '_jpype' extension
error: Unable to find vcvarsall.bat
----------------------------------------
Command "c:\users\xyb-c308\appdata\local\programs\python\python35\python.exe -u
-c "import setuptools, tokenize;__file__='C:\\Users\\xyb-C308\\AppData\\Local\\T
emp\\pip-build-cnhdb_jq\\jpype1\\setup.py';f=getattr(tokenize, 'open', open)(__f
ile__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__
, 'exec'))" install --record C:\Users\xyb-C308\AppData\Local\Temp\pip-h1e28641-r
ecord\install-record.txt --single-version-externally-managed --compile" failed w
ith error code 1 in C:\Users\xyb-C308\AppData\Local\Temp\pip-build-cnhdb_jq\jpyp
说明没有安装visual C++编译器。
这个网址上下载安装Visual C++ Build Tools 2015。再执行pip install jpype1就能完成jpype的安装。
阅读(...) 评论()}

我要回帖

更多关于 linux中安装nginx 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信