当前位置:首页 » 编程语言 » python执行java

python执行java

发布时间: 2022-11-30 13:56:23

python 调用java 是每一次都启动jvm吗

是的

一、JPype简述

1.JPype是什么?

JPype是一个能够让 python 代码方便地调用 Java 代码的工具,从而克服了 python 在某些领域(如服务器端编程)中的不足。

2.JPype与Jython(JPython后继者)的区别?

1)运行环境不同:jython运行在jvm上,而JPype的实际运行环境仍然是python runtime,只是在运行期间启动了一个嵌入的jvm;

2)使用者不同:jython是给java程序玩的,JPype是给python程序员玩的。

二、JPype安装

1.先安装Python2.7和JAVA1.6

2.安装JPype-0.5.4.2.win32-py2.7.exe(http://sourceforge.net/projects/jpype/files/JPype/0.5.4/)

3.Ubuntu12.04安装命令:sudo apt-get install python-jpype

三、JPype使用说明

1.启动JVM

JPype 提供的 startJVM() 函数的作用是启动 JAVA 虚拟机,所以在后续的任何 JAVA 代码被调用前,必须先调用此方法启动 JAVA 虚拟机。

  • jpype.startJVM() 的定义

    startJVM(jvm, *args)

  • jpype.startJVM() 的参数

    参数 1: jvm, 描述你系统中 jvm.dll 文件所在的路径,如“ C:Program FilesIBMJava50jreinj9vmjvm.dll ”。可以通过调用 jpype.getDefaultJVMPath() 得到默认的 JVM 路径。

    参数 2: args, 为可选参数,会被 JPype 直接传递给 JVM 作为 Java 虚拟机的启动参数。此处适合所有合法的 JVM 启动参数,例如:

    -agentlib:libname[=options]
    -classpath classpath
    -verbose
    -Xint

  • 2.关闭JVM

    当使用完 JVM 后,可以通过 jpype.shutdownJVM() 来关闭 JVM,该函数没有输入参数。当 python 程序退出时,JVM 会自动关闭。

    3.引用第三方Java扩展包

    很多时候,在 python 项目中需要调用第三方的 Java 扩展包,这也是 JPype 的一个重要用途。

    通过在 JVM 启动参数增加:-Djava.class.path=ext_classpath,实现在 python 代码中调用已有的 Java 扩展包。

    4.访问JAVA的系统属性

    有时,某些 Java 应用需要设置或者获取 JVM 中的系统属性。

  • 在 JVM 启动时设置系统变量示例:

  • 在 JVM 的启动参数中加入如下参数:
    -Dproperty=value

  • 四、举例

    1.直接调用JAVA API

    [java]view plain

  • fromjpypeimport*

  • importos.path

  • startJVM("C:/Java/jdk1.6.0_10/jre/bin/client/jvm.dll","-ea")

  • java.lang.System.out.println("helloWorld")

  • shutdownJVM()



  • 2.调用JAVA第三方扩展包

    1)JAVA自定义第三方jar包:将JpypeDemo类打包为jpypedemo.jar文件并存储到F:/sample_Py目录下

    [java]view plain

  • packagejpype;

  • publicclassJpypeDemo{

  • publicStringsayHello(Stringuser){

  • return"hello"+user;

  • }

  • publicintcalc(inta,intb){

  • returna+b;

  • }

  • }



  • 2)Python调用第三方JAVA jar包程序

    [python]view plain

  • fromjpypeimport*

  • importos.path

  • jarpath=os.path.join(os.path.abspath('.'),'F:/sample_Py/')

  • startJVM("C:/Java/jdk1.6.0_10/jre/bin/client/jvm.dll","-ea","-Djava.class.path=%s"%(jarpath+'jpypedemo.jar'))

  • #ubuntu 中startJVM("/home/geek/Android/jdk1.6.0_43/jre/lib/i386/server/libjvm.so","-ea","-Djava.class.path=%s"%(jarpath+'XXX.jar'))

  • JDClass=JClass("jpype.JpypeDemo")

  • jd=JDClass()

  • #jd=JPackage("jpype").JpypeDemo()#两种创建jd的方法

  • jprint=java.lang.System.out.println

  • jprint(jd.sayHello("waw"))

  • jprint(jd.calc(2,4))

  • shutdownJVM()



  • 3.访问JAVA的系统属性

    假设你要设置的属性名为 yourProperty,属性值为 yourValue 。

    1)JVM启动时设置系统变量示例

    import jpype
    jvmPath = jpype.getDefaultJVMPath()
    jvmArg = “ -DyourProperty=yourValue ”
    if not jpype.isJVMStarted():
    jpype.startJVM(jvmPath,jvmArg)

    2)在程序中设置系统变量示例

    import jpype
    prop = “ yourProperty ”
    value = “ yourValue ”
    system = jpype.JClass('java.lang.System')
    system.setProperty(str(prop),str(value))

    3)在程序中获取系统变量示例

    import jpype
    prop = “ yourProperty ”
    system = jpype.JClass('java.lang.System')
    value = system.getProperty(str(prop))

Ⅱ python怎么调用java程序

  • 把java封装成restful接口,然后python通过远程调用数据。

  • 使用Pyjnius这个python库。

#源代码:github.com/kivy/pyjnius
#文档:pyjnius.readthedocs.org
#也有其他一些的库,如JPype或Py4j,它们在设计和可用性方面都不是很好。而使用Jython也不为另一种选择,因为我们想使用python开发Android项目。
#现在就让我来告诉你,如何简单的使用Pyjnius:
>>>fromjniusimportautoclass
>>>Stack=autoclass('java.util.Stack')
>>>stack=Stack()
>>>stack.push('hello')
>>>stack.push('world')
>>>stack.pop()
'world'
>>>stack.pop()
'hello'

Ⅲ 怎么使用java运行python脚本

1.直接执行Python脚本代码
引用 org.python包
1 PythonInterpreter interpreter = new PythonInterpreter();
2 interpreter.exec("days=('mod','Tue','Wed','Thu','Fri','Sat','Sun'); "); ///执行python脚本


2. 执行python .py文件
1 PythonInterpreter interpreter = new PythonInterpreter();
2 InputStream filepy = new FileInputStream("D:\\demo.py");
3 interpreter.execfile(filepy); ///执行python py文件
4 filepy.close();


3. 使用Runtime.getRuntime()执行脚本文件
这种方式和.net下面调用cmd执行命令的方式类似。如果执行的python脚本有引用第三方包的,建议使用此种方式。使用上面两种方式会报错java ImportError: No mole named arcpy。
1 Process proc = Runtime.getRuntime().exec("python D:\\demo.py");
2 proc.waitFor();

Ⅳ 用java执行python

1.直接执行Python脚本代码
引用 org.python包
1 PythonInterpreter interpreter = new PythonInterpreter();
2 interpreter.exec("days=('mod','Tue','Wed','Thu','Fri','Sat','Sun'); "); ///执行python脚本

2. 执行python .py文件
1 PythonInterpreter interpreter = new PythonInterpreter();
2 InputStream filepy = new FileInputStream("D:\\demo.py");
3 interpreter.execfile(filepy); ///执行python py文件
4 filepy.close();

3. 使用Runtime.getRuntime()执行脚本文件
这种方式和.net下面调用cmd执行命令的方式类似。如果执行的python脚本有引用第三方包的,建议使用此种方式。使用上面两种方式会报错java ImportError: No mole named arcpy。
1 Process proc = Runtime.getRuntime().exec("python D:\\demo.py");
2 proc.waitFor();

Ⅳ 编程语言Python和Java哪个简单呀

编程语言Python和Java相比较而言,Python更简单一点。以下内容会详细介绍两款编程语言。

总之,Python 和Java 都是功能很强大的编程语言,建议新手先从Python 开始学。

Ⅵ python 调用java对象

你使用jython这个解释器就可以让python直接调用java, 调用完成后,你用python封装成一个服务。其它的python程序员就可以间接调用java对象了。

jython调用java这个方式也被eclipse+pydev使用,是目前最直接的方法。

Ⅶ 怎么在java的flink中调用python程序

一、在java类中直接执行python语句

import org.python.util.PythonInterpreter;
public class FirstJavaScript {
public static void main(String args[]) {

PythonInterpreter interpreter = new PythonInterpreter();

interpreter.exec("days=('mod','Tue','Wed','Thu','Fri','Sat','Sun'); ");
interpreter.exec("print days[1];");

}// main
}

调用的结果是Tue,在控制台显示出来,这是直接进行调用的。

二、在java中调用本机python脚本中的函数

首先建立一个python脚本,名字为:my_utils.py

def adder(a, b):
return a + b
然后建立一个java类,用来测试,

java类代码 FirstJavaScript:

import org.python.core.PyFunction;
import org.python.core.PyInteger;
import org.python.core.PyObject;
import org.python.util.PythonInterpreter;

public class FirstJavaScript {
public static void main(String args[]) {

PythonInterpreter interpreter = new PythonInterpreter();
interpreter.execfile("C:\\Python27\\programs\\my_utils.py");
PyFunction func = (PyFunction) interpreter.get("adder",
PyFunction.class);

int a = 2010, b = 2;
PyObject pyobj = func.__call__(new PyInteger(a), new PyInteger(b));
System.out.println("anwser = " + pyobj.toString());

}// main
}

得到的结果是:anwser = 2012

三、使用java直接执行python脚本

建立脚本inputpy

#open files

print 'hello'
number=[3,5,2,0,6]
print number
number.sort()
print number
number.append(0)
print number
print number.count(0)
print number.index(5)

建立java类,调用这个脚本:

import org.python.util.PythonInterpreter;

public class FirstJavaScript {
public static void main(String args[]) {

PythonInterpreter interpreter = new PythonInterpreter();
interpreter.execfile("C:\\Python27\\programs\\input.py");
}// main
}

得到的结果是:
hello
[3, 5, 2, 0, 6]
[0, 2, 3, 5, 6]
[0, 2, 3, 5, 6, 0]
2
3

热点内容
在linuxpython 发布:2024-04-27 22:38:57 浏览:315
机顶盒密码是在哪里 发布:2024-04-27 22:32:47 浏览:157
名图买哪个配置值得买 发布:2024-04-27 22:32:36 浏览:877
比亚迪秦pro选哪个配置好 发布:2024-04-27 22:32:34 浏览:533
logn算法 发布:2024-04-27 21:58:36 浏览:596
11选五的简单算法 发布:2024-04-27 21:46:14 浏览:71
ebay图片上传 发布:2024-04-27 21:31:50 浏览:587
微信电脑登录显示服务器错误 发布:2024-04-27 20:58:08 浏览:135
压缩弹簧安装 发布:2024-04-27 20:35:43 浏览:371
淘宝视频无法上传视频 发布:2024-04-27 20:31:27 浏览:643