当前位置:首页 » 编程语言 » pythonjava调用

pythonjava调用

发布时间: 2022-06-11 14:37:45

python 调用java对象

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

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

② 如何在 Java 中调用 Python 代码

可以用Python的扩展来实现。Python本来是C实现的,封装二进制兼容的C++是很容易的。Java的话得通过JNI来实现,就是说在Python扩展里用C调用Java。另外,也可以写一个TCP服务来包装C++/Java的接口,通过网络来调用,这样更通用。

③ 怎么在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

④ java调用python时传递的参数问题

需要用到需要用到jython.jar
java example:

public static void main(String[] args) {
//定义参数
String[] args2 = {"arg1","arg2"};
//设置参数
PythonInterpreter.initialize(null, null, args2);
PythonInterpreter interpreter = new PythonInterpreter();
//执行
interpreter.execfile("E:\\jython.py");
System.out.println("----------run over!----------");
}
python的程序:
#!/bin/env python
import time
import sys
argCount = len(sys.argv)
print('before sleep')
time.sleep(5);
print('after sleep')
for str in sys.argv:
print(str)

⑤ 有没有从Python调用Java的好方法

[java]view plain

  • String[]arg=newString[]{"python",types,parameter};//第一个参数是python解释器位置,第二个参数是执行的python脚本位置,接下来的都是参数

  • Processprocess=Runtime.getRuntime().exec(arg);

  • =newInputStreamReader(process.getInputStream());

  • BufferedReaderbufferedReader=newBufferedReader(inputStreamReader);

  • Stringline="";

  • StringBufferstringBuffer=newStringBuffer();

  • while((line=bufferedReader.readLine())!=null){

  • stringBuffer.append(line);

  • stringBuffer.append(" ");

  • }

  • bufferedReader.close();

  • process.waitFor();

⑥ python怎样调用Java中类

这个有几种方式,你看看哪种更适合你。

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

  2. 使用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'

如果解决了您的问题请采纳!
如果未解决请继续追问!

⑦ python 怎么 调用 自定义的 java 类

这个有几种方式,你看看哪种更适合你。
把java封装成restful接口,然后python通过远程调用数据。
使用Pyjnius这个python库。

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

⑧ 如何在java中调用python

package com.lyz.test.jython;
import org.python.util.PythonInterpreter;
/**
* 第一个Jython程序
* @author liuyazhuang
*
*/
public class FirstJythonScript {
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];");
}
}

⑨ 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'
热点内容
android设置静音 发布:2025-02-07 20:11:53 浏览:696
bin存储 发布:2025-02-07 20:00:50 浏览:203
android加载界面 发布:2025-02-07 19:55:28 浏览:870
好矿云服务器 发布:2025-02-07 19:54:31 浏览:949
java电话簿 发布:2025-02-07 19:49:26 浏览:797
超级脚本制作 发布:2025-02-07 19:31:30 浏览:487
怎么查看支付宝的账号密码 发布:2025-02-07 19:26:48 浏览:17
惠普服务器查看ip指令 发布:2025-02-07 19:26:47 浏览:435
算法设计模式 发布:2025-02-07 19:15:52 浏览:746
服务器1u能连接几台电脑 发布:2025-02-07 18:50:02 浏览:154