Today encountered Java calls a Python script problem, tangled up for most of the day, encountered various problems. Most of the online search is using Jython, but I want to invoke the Python script has import urllib, this urllib is not a third-party extension library, under the Python installation path under the Lib, under the Python command line must be able to find. But with Jython, the SYS path is too small. Example code:
Import Org.python.core.py;import Org.python.core.pysystemstate;import Org.python.util.pythoninterpreter;public Class Test3 { /** * @param args * /public static void main (string[] args) { pythoninterpreter Interpreter = new Pythoninterpreter (); Pysystemstate sys = py.getsystemstate (); Sys.path.add ("D:\\jython2.5.2\\lib"); System.out.println (Sys.path.toString ()); Interpreter.exec ("print ' Hello '"); Interpreter.exec ("Import sys"); Interpreter.exec ("Print Sys.path"); interpreter.exec ("Import Urllib");// interpreter.exec ("Print Urllib");} }
The printed Sys.path are:
TXT code
[' D:\\eclipse_jee_workspace\\zlabtest\\lib\\lib ', ' __classpath__ ', ' __pyclasspath__/']
Only the path to the Eclipse project is contained here, so of course we can't find urllib. The print Sys.path at the command line is:
With Jython poor Lib library less too much, also lazy to use similar sys.path.add ("D:\\jython2.5.2\\lib"); one adds, so decisively give up Jython.
Then you can use Runtime.getruntime (). EXEC ("Python test.py"); The sample code is as follows:
Import Java.io.bufferedreader;import Java.io.inputstreamreader;public class Test5 {public static void main (String [] args) { try{ System.out.println ("start"); Process PR = runtime.getruntime (). EXEC ("Python test.py"); BufferedReader in = new BufferedReader (new InputStreamReader (Pr.getinputstream ())); String Line; while (line = In.readline ())! = null) { System.out.println (line); } In.close (); Pr.waitfor (); System.out.println ("End"); } catch (Exception e) { e.printstacktrace ();}} }
The contents of the test.py file are:
Python code
Import sys import urllib print "Hello" print Sys.path
The result of the Java program running is:
TXT code
Start Hello [' d:\\eclipse_jee_workspace\\zlabtest ', ' c:\\windows\\system32\\python27.zip ', ' d:\\python27 \\DLLs ', ' d:\\python27\\lib ', ' D:\\python27\\lib\\plat-win ', ' d:\\python27\\lib\\lib-tk ', ' d:\\python27 ', ' D:\\ Python27\\lib\\site-packages ']end
That's the right comparison. But there are a lot of problems in the middle, and running the above Java program throws an exception in eclipse:
Java.io.IOException:Cannot Run Program "python": CreateProcess error=2,ϵͳõҳ»µ½ָ¶
At Java.lang.ProcessBuilder.start (processbuilder.java:460)
At Java.lang.Runtime.exec (runtime.java:593)
At Java.lang.Runtime.exec (runtime.java:431)
At Java.lang.Runtime.exec (runtime.java:328)
At Com.mysrc.Test5.main (test5.java:10)
caused By:java.io.IOException:CreateProcess error=2,ϵͳõҳ»µ½ָ¶
At Java.lang.ProcessImpl.create (Native Method)
At Java.lang.processimpl.<init> (processimpl.java:81)
At Java.lang.ProcessImpl.start (processimpl.java:30)
At Java.lang.ProcessBuilder.start (processbuilder.java:453)
... 4 more
It is impossible to invoke the Python program, and if it is compiled with Javac at the command line, then Java is definitely right. How can in eclipse also normal operation, on-line check for half a day, in run configurations->environment create a new path, the value is set to install Python path, then run OK.