zoukankan      html  css  js  c++  java
  • java和python互相调用

     

    java和python互相调用

     作者:xuaijun
     日期:2017.1.1

            python作为一种脚本语言,大量用于测试用例和测试代码的编写,尤其适用于交互式业务场景。实际应用中,很多网管系统做的如交换机、防火墙等设备升级,往往和设备交互的命令以及设备回显的信息都是在python脚本中写好,java工程直接调用相应的python脚本,执行升级的具体步骤。但是具体的命令一般通过和设备建立的ssh连接交互,最后python又重新调回java实现的ssh功能。

            Jython是一种完整的语言,而不是一个Java翻译器或仅仅是一个Python编译器,它是一个Python语言在Java中的完全实现。Jython也有很多从CPython中继承的模块库。最有趣的事情是Jython不像CPython或其他任何高级语言,它提供了对其实现语言的一切存取。所以Jython不仅给你提供了Python的库,同时也提供了所有的Java类。

            java和python的互相调用,就需要用到Jython,可在该连接下载Jython相关jar包:https://repo1.maven.org/maven2/org/python/jython

    Jython环境类

    [java] view plain copy
     
    1. package com.webim.test.jython;  
    2.   
    3. import org.python.core.PySystemState;  
    4. import org.python.util.PythonInterpreter;  
    5.   
    6. /** 
    7.  * Jython环境,生存python解释器 
    8.  * @author webim 
    9.  * 
    10.  */  
    11. public final class JythonEnvironment  
    12. {  
    13.     private static JythonEnvironment INSTANCE = new JythonEnvironment();  
    14.       
    15.     /** 
    16.      * 私有构造方法 
    17.      */  
    18.     private JythonEnvironment()  
    19.     {  
    20.     }  
    21.       
    22.     /** 
    23.      * 获取单例 
    24.      * @return JythonEnvironment 
    25.      */  
    26.     public static JythonEnvironment getInstance()  
    27.     {  
    28.         return INSTANCE;  
    29.     }  
    30.       
    31.     /** 
    32.      * 获取python系统状态,可根据需要指定classloader/sys.stdin/sys.stdout等 
    33.      * @return PySystemState 
    34.      */  
    35.     private PySystemState getPySystemState()  
    36.     {  
    37.         PySystemState.initialize();  
    38.         final PySystemState py = new PySystemState();  
    39.         py.setClassLoader(getClass().getClassLoader());  
    40.         return py;  
    41.     }  
    42.       
    43.     /** 
    44.      * 获取python解释器 
    45.      * @return PythonInterpreter 
    46.      */  
    47.     public PythonInterpreter getPythonInterpreter()  
    48.     {  
    49.         PythonInterpreter inter = new PythonInterpreter(null, getPySystemState());  
    50.         return inter;  
    51.     }  
    52. }  

    java调用python

    [java] view plain copy
     
    1. package com.webim.test.jython;  
    2.   
    3. import java.util.Map;  
    4. import java.util.Map.Entry;  
    5.   
    6. import org.python.util.PythonInterpreter;  
    7. /*enum的这个用法,可以作为变种的安全单例,值得借鉴哦 ^_^ */  
    8. public enum ExecPython  
    9. {  
    10.     INSTANCE;  
    11.       
    12.     public void execute(String scriptFile, Map<String,String> properties)  
    13.     {  
    14.         //获取python解释器  
    15.         final PythonInterpreter inter = JythonEnvironment.getInstance().getPythonInterpreter();  
    16.           
    17.         //设置python属性,python脚本中可以使用  
    18.         for (Entry<String,String> entry : properties.entrySet())  
    19.         {  
    20.             inter.set(entry.getKey(), entry.getValue());  
    21.         }  
    22.           
    23.         try  
    24.         {  
    25.             //通过python解释器调用python脚本  
    26.             inter.execfile(scriptFile);  
    27.         }  
    28.         catch (Exception e)  
    29.         {  
    30.             System.out.println("ExecPython encounter exception:" + e);  
    31.         }  
    32.     }  
    33. }  

    供python调用的java类

    [java] view plain copy
     
    1. package com.webim.test.jython;  
    2.   
    3. /** 
    4.  * 供python脚本调用的java类 
    5.  * @author webim 
    6.  * 
    7.  */  
    8. public class SayHello  
    9. {  
    10.     private String userName;  
    11.   
    12.     public String getUserName()  
    13.     {  
    14.         return userName;  
    15.     }  
    16.   
    17.     public void setUserName(String userName)  
    18.     {  
    19.         this.userName = userName;  
    20.     }  
    21.       
    22.     public void say(int i)  
    23.     {  
    24.         System.out.println(i + ":Hello " + userName);  
    25.     }  
    26. }  

    测试代码

    [java] view plain copy
     
    1. package com.webim.test.jython;  
    2.   
    3. import java.util.HashMap;  
    4. import java.util.Map;  
    5.   
    6. /** 
    7.  * 测试java和python的调用流程 
    8.  * @author webim 
    9.  * 
    10.  */  
    11. public enum TestExecPython  
    12. {  
    13.     INSTANCE;  
    14.       
    15.     public void test()  
    16.     {  
    17.         String scriptFile = "test.py";  
    18.         Map<String,String> properties = new HashMap<String,String>();  
    19.         properties.put("userName", "Demo");  
    20.           
    21.         ExecPython.INSTANCE.execute(scriptFile, properties);  
    22.     }  
    23. }  

    main方法类

    [java] view plain copy
     
    1. package com.webim.test.main;  
    2.   
    3. import com.webim.test.jython.TestExecPython;  
    4.   
    5. public class Main   
    6. {  
    7.     public static void main(String[] args)   
    8.     {  
    9.         TestExecPython.INSTANCE.test();  
    10.     }  
    11.   
    12. }  
    python脚本
    [python] view plain copy
     
    1. #unicode=UTF-8  
    2.   
    3. #################################################  
    4. #通过java package导入java类  
    5. from com.webim.test.jython import SayHello  
    6.   
    7. execpy = SayHello()  
    8.   
    9. #################################################  
    10. #将python属性传入后续调用的java实例  
    11. execpy.setUserName(userName)  
    12.   
    13. def say():  
    14.     execpy.say(5)  
    15.     return  
    16.   
    17. say()  
  • 相关阅读:
    【leetcode】106. Construct Binary Tree from Inorder and Postorder Traversal
    【leetcode】105. Construct Binary Tree from Preorder and Inorder Traversal
    【leetcode】236. Lowest Common Ancestor of a Binary Tree
    【leetcode】235. Lowest Common Ancestor of a Binary Search Tree
    【leetcode】352. Data Stream as Disjoint Intervals
    【leetcode】897. Increasing Order Search Tree
    【leetcode】900. RLE Iterator
    BEC listen and translation exercise 26
    BEC listen and translation exercise 25
    BEC listen and translation exercise 24
  • 原文地址:https://www.cnblogs.com/xuaijun/p/7986003.html
Copyright © 2011-2022 走看看