How to use Jython in java
Most people do not understand the knowledge points of this article "how to use Jython in java", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use Jython in java" article.
What is Jython
Jython is a pure Java implementation of Python. She seamlessly combines Java classes with Python, enabling users to write software that runs on Java virtual machines in the syntax of the Python language. Its features are: compared with similar Java programs, Jython greatly reduces the amount of programming code. Jython has both an interpreter and a compiler so that it can test program code without compiling.
Second, use step 1. Introduce dependency
The code is as follows (example):
Org.python jython-standalone 2.7.0 2. Call code / / function: find the corresponding encrypted data from word. The encryption algorithm is hash.md5_crypt / / original data List word = new ArrayList (); word.add ("123"); word.add ("456"); / / encrypted data List cryptWord = new ArrayList (); cryptWord.add ("$1 $KP074k5L$GkgfZVwByM0FQt4l.KLoh/") CryptWord.add ("$1 $zTxoz1fL$HKSbEyNFHGkLgAHZUTjmz."); String pythonFilePath = "jython_test.py"; String pythonFileMethod = "verify"; PythonInterpreter interpreter = new PythonInterpreter (); ClassPathResource resource = new ClassPathResource (pythonFilePath); InputStream inputStream = resource.getInputStream (); interpreter.execfile (inputStream); PyFunction verify = interpreter.get (pythonFileMethod, PyFunction.class) / / call PyObject pyObject = verify.__call__ (new PyList (word), new PyList (cryptWord)); List result = (List) pyObject.__tojava__ (List.class); System.out.println (result); interpreter.close ()
Output result:
['word:456, crypt_word:1 11KP074k5L' GkgfZVwByM0FQt4l.KLoh, 'word:123, crypt_word:1 11zTxoz1fL' HKSbEyNFHGkLgAHZUTjmz.']
2.python script from passlib.hash import md5_cryptdef verify (word,crypt_word): result= [] for crypt_w in crypt_word: for w in word: if md5_crypt.verify (wPowerCryptExw): item = 'word: {}, crypt_word: {}' .format (wmai cryptfolw) result.append (item) break return result III, question 1. Error report: ImportError: No module named passlib
The error indicates that the passlib library is not installed, then you need to import the passlib library before you can use from passlib.hash import md5_crypt
Can be installed on linux through the pip install passlib command
Windows: for example, you can use spyder to perform a pip install passlib installation
If you still report an error after installation, it may be because the library installation path is not in path, so you need to introduce the installation path in the script, for example:
Import syssys.path.append ('D:\ tools\ Anaconda\ lib\ site-packages')
Or introduce it through code:
Interpreter.exec ("import sys"); interpreter.exec ("sys.path.append ('D:\ tools\ Anaconda\ lib\ site-packages')"); 2. Error report: Cannot create PyString with non-byte value
You can find the error in the source code:
Public PyString (PyType subType, String string) {super (subType); if (string = = null) {throw new IllegalArgumentException ("Cannot create PyString from null");} else if (! isBytes (string)) {throw new IllegalArgumentException ("Cannot create PyString with non-byte value");} this.string = string;}
Then enter the isBytes (string) method:
Private static boolean isBytes (String s) {int k = s.length (); if (k = = 0) {return true;} else {char c = 0; / / while (k > 8) {c | = s.charAt (--k); c | = s.charAt (--k) C | = s.charAt (--k); c | = s.charAt (--k) } / / calculate the last while of less than 8 times (k > 0) {c | = s.charAt (--k);} / / compare the size return c < 0x100;}}
This method performs the summation of each character on the incoming string, and the final result is less than 0x100, that is to say, the size of each character cannot exceed 256.
The reason why I reported an error here is that the path of the python file passed in when I created the PythonInterpreter is in Chinese.
The above is about the content of this article on "how to use Jython in java". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.