Java and Python call each other
Python, as a scripting language, is used extensively for the writing of test cases and test code, especially for interactive business scenarios. In practical applications, many network management systems such as switches, firewalls and other equipment upgrades, often with the device interaction commands and device echo information are written in the Python script, the Java project directly invoke the corresponding Python script, perform the specific steps of the upgrade. However, the specific commands typically interact with the SSH connection established by the device, and finally Python is re-recalled to the Java-implemented SSH functionality.
Jython is a complete language, not a Java translator or just a Python compiler, which is a full implementation of the Python language in Java. Jython also has many module libraries that inherit from CPython. The most interesting thing is that Jython is not like CPython or any other high-level language, it provides all access to its implementation language. So Jython not only provides you with a Python library, but also provides all of the Java classes.
Java and Python call each other, you need to use Jython, you can download the Jython related jar package on the connection: Https://repo1.maven.org/maven2/org/python/jython.
Less nonsense, the simplest example is straightforward.
Jython Environment Class
[Java]View PlainCopy
- Package Com.webim.test.jython;
- Import Org.python.core.PySystemState;
- Import Org.python.util.PythonInterpreter;
- /**
- * Jython environment, Survival python interpreter
- * @author Webim
- *
- */
- Public Final class Jythonenvironment
- {
- private static jythonenvironment INSTANCE = new Jythonenvironment ();
- /**
- * Private Construction method
- */
- private jythonenvironment ()
- {
- }
- /**
- * Get a single case
- * @return Jythonenvironment
- */
- public static Jythonenvironment getinstance ()
- {
- return INSTANCE;
- }
- /**
- * Get Python System status, specify Classloader/sys.stdin/sys.stdout and so on as needed
- * @return Pysystemstate
- */
- private Pysystemstate getpysystemstate ()
- {
- Pysystemstate.initialize ();
- final pysystemstate py = new Pysystemstate ();
- Py.setclassloader (GetClass (). getClassLoader ());
- return py;
- }
- /**
- * Get Python Interpreter
- * @return Pythoninterpreter
- */
- Public pythoninterpreter Getpythoninterpreter ()
- {
- Pythoninterpreter inter = new Pythoninterpreter (null, getpysystemstate ());
- return Inter;
- }
- }
Java calls Python
[Java]View PlainCopy
- Package Com.webim.test.jython;
- Import Java.util.Map;
- Import Java.util.Map.Entry;
- Import Org.python.util.PythonInterpreter;
- This use of/*enum, can be used as a variant of the safety of the single case, it is worth learning oh ^_^ * *
- Public enum Execpython
- {
- INSTANCE;
- public void Execute (String scriptfile, map<string,string> properties)
- {
- //Get Python interpreter
- final Pythoninterpreter inter = Jythonenvironment.getinstance (). Getpythoninterpreter ();
- //Set Python properties, which can be used in Python scripts
- For (entry<string,string> entry:properties.entrySet ())
- {
- Inter.set (Entry.getkey (), Entry.getvalue ());
- }
- Try
- {
- //Python script is called through the Python interpreter
- Inter.execfile (ScriptFile);
- }
- catch (Exception e)
- {
- System.out.println ("Execpython Encounter Exception:" + E);
- }
- }
- }
Java classes for Python calls
[Java]View PlainCopy
- Package Com.webim.test.jython;
- /**
- * Java classes for Python script calls
- * @author Webim
- *
- */
- Public class SayHello
- {
- private String UserName;
- Public String GetUserName ()
- {
- return userName;
- }
- public void Setusername (String userName)
- {
- this.username = userName;
- }
- public void say (int i)
- {
- System.out.println (i + ": Hello" + userName);
- }
- }
Test code
[Java]View PlainCopy
- Package Com.webim.test.jython;
- Import Java.util.HashMap;
- Import Java.util.Map;
- /**
- * Test the call flow of Java and Python
- * @author Webim
- *
- */
- Public enum Testexecpython
- {
- INSTANCE;
- public Void Test ()
- {
- String scriptfile = "test.py";
- map<string,string> properties = new hashmap<string,string> ();
- Properties.put ("UserName", "Demo");
- ExecPython.INSTANCE.execute (ScriptFile, properties);
- }
- }
Main Method Class
[Java]View PlainCopy
- Package com.webim.test.main;
- Import Com.webim.test.jython.TestExecPython;
- Public class Main
- {
- public static void Main (string[] args)
- {
- TestExecPython.INSTANCE.test ();
- }
- }
Python script
[Python]View PlainCopy
- #unicode =utf-8
- #################################################
- Importing Java Classes #通过java package
- From Com.webim.test.jython import SayHello
- execpy = SayHello ()
- #################################################
- #将python属性传入后续调用的java实例
- Execpy.setusername (UserName)
- Def say ():
- Execpy.say (5)
- return
- Say ()
Java and Python call each other