Because of work needs , It's easy to learn , I'm using IDEA ,jdk 1.8
First, configure locally Python Environmental Science , It's used here Anaconda, Choose to add environment variables during installation , When it's done cmd Interface input python command , You can see python Version information for , thus , Already completed python Environmental installation
Next , test python Statement running , Bring one of your own Python Code file to run
python XXXX\XX\xxx.py, as follows :
Come here , You can find ,python Scripts can be run
Next , go back to IDEA
The code is as follows :
1 public static void main(String[] args) { 2 try { 3 String exe = "python"; 4 String command = "XXX\\redis-demo.py"; 5 String[] cmdArr = new String[]{exe, command}; 6 Process process = Runtime.getRuntime().exec(cmdArr); 7 BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream(),"GBK")); 8 String line; 9 while ((line = in.readLine()) != null) { 10 System.out.println(line); 11 } 12 in.close(); 13 int result = process.waitFor(); 14 System.out.println(" Execution results :" + result); 15 } catch (IOException | InterruptedException e) { 16 e.printStackTrace(); 17 } 18 19 }
This is actually local Process The way it's called , The processing logic is java Call... By calling the command line Python Program , Relative to those who have stopped maintaining updates Jpython, Can handle Python Various packages imported in the program , And run faster .