tags: JAVA python Development language
1. First introduced Maven dependence
Can be logged inMavenWebsitejython-standaloneDependent, selecting a lot of uses to use, this is 2.7.0 as an example:
<dependency>
<groupId>org.python</groupId>
<artifactId>jython-standalone</artifactId>
<version>2.7.0</version>
</dependency>
2. Write the Python function, here Pycharm is an example, write a fast row, and can be used after testing
def partition(arr, low, high):
i = (low-1)
pivot = arr[high]
for j in range(low, high):
if arr[j] <= pivot:
i = i + 1
arr[i], arr[j] = arr[j], arr[i]
arr[i+1], arr[high] = arr[high], arr[i+1]
return i + 1
def quick_sort(arr, low, high):
if low < high:
pi = partition(arr, low, high)
quick_sort(arr, low, pi-1)
quick_sort(arr, pi + 1, high)
#
def do_sort(nums):
arr = nums
n = len(arr)
quick_sort(arr, 0, n-1)
return arr
# if __name__ == '__main__':
# nums = [56, 8, 4, 23, 5, 19]
# res = do_sort(nums)
# print(res)
3. Write the Java code to call the above fast row
package com.example.graduation.controller;
import com.example.graduation.bean.data;
import com.example.graduation.service.DataService;
import org.python.core.PyFunction;
import org.python.core.PyList;
import org.python.core.PyObject;
import org.python.util.PythonInterpreter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
@Controller
public class DataController {
/ / The focus is this function
@RequestMapping("/test")
@ResponseBody
public String test(){
/ / Generate an object that can call Python
PythonInterpreter interpreter = new PythonInterpreter();
/ / Generate a random number within 100
Random random = new Random();
List<Integer> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
list.add(random.nextInt(100));
}
// Call Python file
interpreter.execfile("F:\\WK\\Python\\experiment\\graduation\\test.py");
PyFunction function = interpreter.get("do_sort", PyFunction.class);
PyObject obj = function.__call__(new PyList(list));
return obj.toString();
}
}
4. Start the SpringBoot project

5. Browser Access http: // localhost: 8080 / TEST

JS is as follows: The JAVA code is as follows: The result of the call is as follows:...
There are five types of instructions that are invoked by the JVM method: invokestatic Call class methods (static binding, fast) invokevirtual Call instance method (dynamic binding) invokespecial Call ...
Two implementations Java_ package nameClass nameMethod name RegisterNatives method Native method is as follows The first way: in the Native should (using the c++ method) This method is not possible if...
There are four forms of unity calling java methods, namely: Common method without return value Common method with return value Static method with no return value Static method with return value Passin...
Java method The difference between methods and functions In order to distinguish, let's not discussstaticmethod. A method is an object-oriented concept that relies on objects and functions are not. As...
When using Java reflection, method calls, you may encounter the most problems is how variable method of use. In fact, all the variables in the calling method parameter array, no matter how many argume...
First, this method calls the class A method, call the method is declared as static, you can directly call other methods. Sample code is as follows: The second method is called method is not static mod...
Article Directory First, the assignment 1.1 Static assignment (overloaded) 1.2 Dynamic dispatching (rewritable) 1.3 Single and multiple dispatch dispatch Second, a dynamically typed language support 2...
One, jstl function jstl function customization Second, the value of the calling method becomes a variable The first use of request object The second use of label variables Convert to variable use <...
Java calls Native method Write Java classes and declare Native methods Generate **.h header file First compile the Java class javac Student.java into a class file Use JDK's own jni to generate **.h he...