Java Invoke Python description document

Source: Internet
Author: User
Tags stdin python script

Java called python Description Document

There are three main ways to call Python scripts through Java:

1. Execute Python script directly;

2, the implementation of python.py documents;

3. Execute the script file using runtime.getruntime () ;

The first two are the compilers that introduce Jython calls Pythoninterpreter, but if you call some of Python's extension libraries, there will be an error, and the machine learning Library is called in this way, and the solution is not found (ask everyone),---------- Maybe it's because of numpy's sake.

The third way is also a very simple way to call. Use Java to invoke the console process, which is runtime.getruntime (). exec (), to run this Python script. The following is a brief introduction to the knowledge used in this way.

I. About P rocess

The process class is an abstract class (all methods are abstract) and encapsulates a process (that is, an executing program).

The process class provides a way to perform input from the process, perform output to the process, wait for the process to complete, check the exit status of the process, and destroy (kill) the process.

The Processbuilder.start () and Runtime.exec methods create a native process and return an instance of the process subclass that can be used to control the process and obtain relevant information.

The method of creating a process may not work well for specific processes on some native platforms, such as native window processes, daemons, win16/dos processes on Microsoft Windows, or shell scripts. The child process created does not have its own terminal or console. All of its standard IO (that is, Stdin,stdout,stderr) operations are redirected to the parent process through three streams (Getoutputstream (), getInputStream (), Geterrorstream ()). The parent process uses these streams to provide input to the child process and to obtain output from the child process. Because some native platforms only provide a limited buffer size for standard input and output streams, it can cause child processes to block and even deadlock if the output stream or input stream of a child process fails quickly. When there are no more references to the process object, instead of deleting the child process, it continues to execute the child process asynchronously.

Second, How to create Process object, generally there are two ways

1) each Processbuilder instance manages a set of process properties. the Start () method uses these properties to create a new Process instance. the Start () method can be called repeatedly from the same instance to create a new child process with the same or related properties.

2) The Runtime.exec () method creates a native process and returns an instance of the process subclass.

Three, the code demonstration

Java Core Code:

1 Process proc=runtime.getruntime (). EXEC (args1// execute py file 23 4 inputstreamreader stdin=new  InputStreamReader (Proc.getinputstream ()); 5 6 // InputStreamReader class for constructing a default encoding set 7 8 linenumberreader input=new LineNumberReader (stdin);//used to read text files by line

where args1 is string[] {"Python", path,url1,url2}; , path is the path to the Python file, URL1 is the parameter 1,url2 is parameter 2.

Python code:

Where SYS.ARGV is used to obtain parameters url1,url2 , etc.

Sys.argv[0]: Is the python script name

SYS.ARG[1] and later: The parameters to be received

Java Invoke Python description document

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.