使用Jpython调用跑一python

1、添加maven依赖

  1. <dependency>  
  2.     <groupId>org.python</groupId>  
  3.     <artifactId>jython</artifactId>  
  4.     <version>2.7.0</version>  
  5. </dependency> 

2、在Java中执行python语句

  1. import javax.script.*;  
  2.   
  3. import org.python.util.PythonInterpreter;  
  4.   
  5. import java.io.*;  
  6. import static java.lang.System.*;  
  7. public class FirstJavaScript  
  8. {  
  9.  public static void main(String args[])  
  10.  {  
  11.     
  12.   PythonInterpreter interpreter = new PythonInterpreter();  
  13.   interpreter.exec("days=('mod','Tue','Wed','Thu','Fri','Sat','Sun'); ");  
  14.   interpreter.exec("print days[1];");  
  15.     
  16.     
  17.  }//main  
  18. }  

2、在Java中执行Python脚本中的函数

新建python脚本complex.py

import addition

def complex(a,b):
    print(addition.add(a,b))
    print(addition.sub(a,b))
    print(addition.multi(a,b))
    print(addition.div(a,b))

a = int(input("input a:"))
b = int(input("input b:"))
complex(a,b)
新建Python脚本addition.py

def add(a,b):
    return a+b

def sub(a,b):
    return a-b

def multi(a,b):
    return a*b

def div(a,b):
    return a/b

Java类代码

  1. import org.python.core.PyFunction;
    import org.python.core.PyInteger;
    import org.python.core.PyObject;
    import org.python.util.PythonInterpreter;
    
    import java.io.IOException;
    import java.util.Properties;
    
    /**
     * Created by zhangmingbo on 3/20/17.
     */
    public class PythonDemo {
    
        public static void main(String[] args) throws IOException {
    
            //Create interpreter
            Properties props = new Properties();
            props.put("python.home", "path to the Lib folder");
            props.put("python.console.encoding", "UTF-8"); // Used to prevent: console: Failed to install '': java.nio.charset.UnsupportedCharsetException: cp0.
            props.put("python.security.respectJavaAccessibility", "false"); //don't respect java accessibility, so that we can access protected members on subclasses
            props.put("python.import.site", "false");
    
            Properties preprops = System.getProperties();
            PythonInterpreter.initialize(preprops, props, new String[0]);
    
            PythonInterpreter interp = new PythonInterpreter();
    
            interp.exec("import sys");
    //        interp.exec("sys.path.append('D:/Program Files (x86)/jython2.7.0/Lib')");//jython自己的
    //        interp.exec("sys.path.append('D:/Program Files (x86)/jython2.7.0/Lib/site-packages')");//jython自己的
            interp.exec("sys.path.append('C:/Users/xuhao/Loquat')");//我们自己写的
    
            interp.execfile("complex.py");
            PyFunction func = (PyFunction)interp.get("complex",PyFunction.class);
            int a = 2010, b = 2 ;
            PyObject pyobj = func.__call__(new PyInteger(a), new PyInteger(b));
            System.out.println("anwser = " + pyobj.toString());
        }
    }

3、使用Java执行python脚本
建立python脚本input.py
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 javax.script.*;  
  
import org.python.core.PyFunction;  
import org.python.core.PyInteger;  
import org.python.core.PyObject;  
import org.python.util.PythonInterpreter;  
  
import java.io.*;  
import static java.lang.System.*;  
public class FirstJavaScript  
{  
 public static void main(String args[])  
 {  
    
  PythonInterpreter interpreter = new PythonInterpreter();  
  interpreter.execfile("C:\\Python27\\programs\\input.py");  
 }//main  
} 







发布了10 篇原创文章 · 获赞 18 · 访问量 5万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览