Python_Java调用Jython
2012-05-07 11:44
405 查看
Java调用jython
准备:
1. sun-jre1.6, jython 2.5
2. 在官网下下个jython_installer-2.5.0.jar,一路next, 在 /jython-install-path/里有个jython.jar, 把这个jython.jar import 进Java Project 里边。
python代码: fibo.py
# Fibonacci numbers module
def fib(n): # write Fibonacci series up to n
a, b = 0, 1
while b < n:
print b,
a, b = b, a+b
def fib2(n): # return Fibonacci series up to n
result = []
a, b = 0, 1
while b < n:
result.append(b)
a, b = b, a+b
return result
import org.python.core.PyException;
import org.python.core.PyFile;
import org.python.core.PyObject;
import org.python.core.PyString;
import org.python.util.PythonInterpreter;
public class TestJython {
/**
* 关注Jython这几个方面的内容:
* 1. 怎样从某个指定的路径import一个Jython模块?
* 2. 怎样调用模块里的方法?
* 3. java 与 jython 间的参数应该怎样进行传递?
*
* @param args
* @throws PyException
*/
public static void main(String []args)throws PyException
{
PythonInterpreter interp = new PythonInterpreter();
// 1.
// 引入系统模块,并将[./pythonsrc]加入模块搜索路径中去。
interp.exec("import sys");
interp.exec("sys.path.append('./pythonsrc')");
// 2. 引入 fibo, 执行fibo.fib(100), ok
interp.exec("import fibo");
interp.exec("fibo.fib(100)");
// 3. 在Java 程序中获得 python内置类型的变量值。
String text = "'this is a bad day'";
interp.exec("x = " + text + " + '!!!'");
interp.exec("print x");
PyObject object = interp.get("x");
PyString xString = object.__str__();
String outX = xString.asString();
interp.exec("_file = open('/home/fore/work/OaasSearch/search/oaas_search/workspace/oaas-search0.1/pythonsrc/fibo.py')");
object = interp.get("_file");
PyFile file = (PyFile)object;
PyString content = file.read(100);
System.out.println(content);
interp.exec("array = _file.read(100)");
String array = interp.get("array").asString();
System.out.println(array);
interp.exec("_file.close()");
file.close();
}
}
按照以上步骤,我的实现如下:
1.通过Java Project 的import 导入安装在C盘安装路径下的jython.jar
2. 建立一个包com.java.jython,创建类javaJython.java,创建python文件fibo.py
3. 源代码修改如下:
fibo.py
# Fibonacci numbers module
def fib(n): # write Fibonacci series up to n
a, b = 0, 1
while b < n:
print b,
a, b = b, a+b
def fib2(n): # return Fibonacci series up to n
result = []
a, b = 0, 1
while b < n:
result.append(b)
a, b = b, a+b
return result
javaJython.java源代码:
package com.java.jython;
import org.python.core.PyException;
import org.python.core.PyFile;
import org.python.core.PyObject;
import org.python.core.PyString;
import org.python.util.PythonInterpreter;
public class javaJython {
/**
* 关注Jython这几个方面的内容:
* 1. 怎样从某个指定的路径import一个Jython模块?
* 2. 怎样调用模块里的方法?
* 3. java 与 jython 间的参数应该怎样进行传递?
*
* @param args
* @throws PyException
*/
public static void main(String []args)throws PyException
{
PythonInterpreter interp = new PythonInterpreter();
// 1.
// 引入系统模块,并将[./com/java/jython]加入模块搜索路径中去。
interp.exec("import sys");
interp.exec("sys.path.append('E:/workspace/androidwork/JavaJython/src/com/java/jython')");
// 2. 引入 fibo, 执行fibo.fib(100), ok
interp.exec("import fibo");
interp.exec("fibo.fib(100)");
// 3. 在Java 程序中获得 python内置类型的变量值。
String text = "'this is a bad day'";
interp.exec("x = " + text + " + '!!!'");
interp.exec("print x");
PyObject object = interp.get("x");
PyString xString = object.__str__();
String outX = xString.asString();
interp.exec("_file = open('E:/workspace/androidwork/JavaJython/src/com/java/jython/fibo.py')");
object = interp.get("_file");
PyFile file = (PyFile)object;
PyString content = file.read(100);
System.out.println(content);
interp.exec("array = _file.read(100)");
String array = interp.get("array").asString();
System.out.println(array);
interp.exec("_file.close()");
file.close();
}
}
4.运行结果如下:
1 1 2 3 5 8 13 21 34 55 89
this is a bad day!!!
# Fibonacci numbers module
import sys
print sys.path
def fib(n): # write Fibonacci series up to
n
a, b = 0, 1
while b < n:
print b,
a, b = b, a+b
def fib2(n): # retur
准备:
1. sun-jre1.6, jython 2.5
2. 在官网下下个jython_installer-2.5.0.jar,一路next, 在 /jython-install-path/里有个jython.jar, 把这个jython.jar import 进Java Project 里边。
python代码: fibo.py
# Fibonacci numbers module
def fib(n): # write Fibonacci series up to n
a, b = 0, 1
while b < n:
print b,
a, b = b, a+b
def fib2(n): # return Fibonacci series up to n
result = []
a, b = 0, 1
while b < n:
result.append(b)
a, b = b, a+b
return result
import org.python.core.PyException;
import org.python.core.PyFile;
import org.python.core.PyObject;
import org.python.core.PyString;
import org.python.util.PythonInterpreter;
public class TestJython {
/**
* 关注Jython这几个方面的内容:
* 1. 怎样从某个指定的路径import一个Jython模块?
* 2. 怎样调用模块里的方法?
* 3. java 与 jython 间的参数应该怎样进行传递?
*
* @param args
* @throws PyException
*/
public static void main(String []args)throws PyException
{
PythonInterpreter interp = new PythonInterpreter();
// 1.
// 引入系统模块,并将[./pythonsrc]加入模块搜索路径中去。
interp.exec("import sys");
interp.exec("sys.path.append('./pythonsrc')");
// 2. 引入 fibo, 执行fibo.fib(100), ok
interp.exec("import fibo");
interp.exec("fibo.fib(100)");
// 3. 在Java 程序中获得 python内置类型的变量值。
String text = "'this is a bad day'";
interp.exec("x = " + text + " + '!!!'");
interp.exec("print x");
PyObject object = interp.get("x");
PyString xString = object.__str__();
String outX = xString.asString();
interp.exec("_file = open('/home/fore/work/OaasSearch/search/oaas_search/workspace/oaas-search0.1/pythonsrc/fibo.py')");
object = interp.get("_file");
PyFile file = (PyFile)object;
PyString content = file.read(100);
System.out.println(content);
interp.exec("array = _file.read(100)");
String array = interp.get("array").asString();
System.out.println(array);
interp.exec("_file.close()");
file.close();
}
}
按照以上步骤,我的实现如下:
1.通过Java Project 的import 导入安装在C盘安装路径下的jython.jar
2. 建立一个包com.java.jython,创建类javaJython.java,创建python文件fibo.py
3. 源代码修改如下:
fibo.py
# Fibonacci numbers module
def fib(n): # write Fibonacci series up to n
a, b = 0, 1
while b < n:
print b,
a, b = b, a+b
def fib2(n): # return Fibonacci series up to n
result = []
a, b = 0, 1
while b < n:
result.append(b)
a, b = b, a+b
return result
javaJython.java源代码:
package com.java.jython;
import org.python.core.PyException;
import org.python.core.PyFile;
import org.python.core.PyObject;
import org.python.core.PyString;
import org.python.util.PythonInterpreter;
public class javaJython {
/**
* 关注Jython这几个方面的内容:
* 1. 怎样从某个指定的路径import一个Jython模块?
* 2. 怎样调用模块里的方法?
* 3. java 与 jython 间的参数应该怎样进行传递?
*
* @param args
* @throws PyException
*/
public static void main(String []args)throws PyException
{
PythonInterpreter interp = new PythonInterpreter();
// 1.
// 引入系统模块,并将[./com/java/jython]加入模块搜索路径中去。
interp.exec("import sys");
interp.exec("sys.path.append('E:/workspace/androidwork/JavaJython/src/com/java/jython')");
// 2. 引入 fibo, 执行fibo.fib(100), ok
interp.exec("import fibo");
interp.exec("fibo.fib(100)");
// 3. 在Java 程序中获得 python内置类型的变量值。
String text = "'this is a bad day'";
interp.exec("x = " + text + " + '!!!'");
interp.exec("print x");
PyObject object = interp.get("x");
PyString xString = object.__str__();
String outX = xString.asString();
interp.exec("_file = open('E:/workspace/androidwork/JavaJython/src/com/java/jython/fibo.py')");
object = interp.get("_file");
PyFile file = (PyFile)object;
PyString content = file.read(100);
System.out.println(content);
interp.exec("array = _file.read(100)");
String array = interp.get("array").asString();
System.out.println(array);
interp.exec("_file.close()");
file.close();
}
}
4.运行结果如下:
1 1 2 3 5 8 13 21 34 55 89
this is a bad day!!!
# Fibonacci numbers module
import sys
print sys.path
def fib(n): # write Fibonacci series up to
n
a, b = 0, 1
while b < n:
print b,
a, b = b, a+b
def fib2(n): # retur
相关文章推荐
- Java调用Python开发环境配置(Eclipse+Jython+PyDev)
- Java通过JYTHON调用python
- Python与Java之间的相互调用——Jython
- Python与Java之间的相互调用——Jython
- Java调用python的方法(jython)
- Jython:java调用python文件之第三方包路径问题
- Java调用python——Jython
- Jython_Java 调用python
- python调用java jython
- Java实现调用jython执行python文件的方法
- java调用python(jython)的报错
- Jython:java调用python文件之第三方包路径问题
- Learning Python -- Java 通过JyThon调用Python实现的规则
- [java]Java调用python工具类(非jython)
- JAVA调用jython执行python文件
- java调用python方法的库jython介绍及使用实例
- Java中调用python程序
- python调用java程序--jpype
- jython实现java运行python代码
- Java调用python