Jython:java How to pass a value to Python

Source: Internet
Author: User
Tags python script

To invoke a Python script in a Java project, the most important thing is that the Python script needs to use the variables that are passed in real time in Java. So trouble, looking around for tutorials.

Chinese tutorial is zero, fortunately there is stack over Flow, input keywords: jython java passing variable Topython, really found a case. The following is the translation of the content:

I'm using jython2.7.0, I want to pass the Java command line parameters through Jython to the Python script, the result is completely unexpected, the code is as follows:

string[] Arguments ={"arg1", "arg2", "Arg3"}; Pythoninterpreter.initialize (Props,system.getproperties (), arguments); Org.python.util.PythonInterpreterpython = New Org.python.util.PythonInterpreter (); StringWriter out = Newstringwriter ();p ython.setout (out);p ython.execfile ("myscript.py"); String outputstr =out.tostring (); System.out.println (OUTPUTSTR);
the problem with the code running is that the pass value failed. This means that the interface is not properly invoked. Here are two answers (not tested):

Author's supplementary reply :

string[] Arguments ={"myscript.py", "Arg1", "arg2", "Arg3"}; Pythoninterpreter.initialize (System.getproperties (), system.getproperties (), arguments); O Rg.python.util.PythonInterpreterpython = new Org.python.util.PythonInterpreter (); StringWriter out = Newstringwriter ();p ython.setout (out);p ython.execfile ("myscript.py"); String outputstr =out.tostring (); System.out.println (OUTPUTSTR);

Notice in the first line that the author passed the script through arg[0].

other people reply :

Import Org.python.core.py;import Org.python.core.pyexception;import Org.python.core.pyobject;import Org.python.core.pystring;importorg.python.core.__builtin__;importorg.python.util.pythoninterpreter; public class Jythontest {public static void main (string[] args) {Pythoninterpreter interpreter = Newpythonint        Erpreter ();        String Fileurlpath = "/path/to/script";        String scriptname = "MyScript"; Interpreter.exec ("importsys\n" + "import OS \ n" + "Sys.path.append ('" +fileurlpath + ") \ n" + "from" +scriptname+ "Import *        \ n ");        String funcName = "MyFunction";        Pyobject somefunc =interpreter.get (funcName);        if (SomeFunc = = null) {throw new Exception ("Could notfind Python function:" + funcName);        } try {somefunc.__call__ (newpystring (arg1), New Pystring (ARG2), New pystring (ARG3));        } catch (Pyexception e) {e.printstacktrace (); }    }}

Note that each other creates a Python script called myscript.py in the Directory/path/to/script directory:

def myscript (ARG1,ARG2,ARG3):    print "Calling Python function withparamters:"    print arg1    print arg2    Print Arg3

Pro-Test:

Importjava.io.ioexception;importorg.python.util.pythoninterpreter;  public class Test {public     static void Main (string[] args) throws IOException {          int[] arg = {3,6};         Pythoninterpreter.initialize (System.getproperties (), System.getproperties (), new string[0]);         Pythoninterpreter interpreter = new Pythoninterpreter ();          Interpreter.exec ("a=" + arg[0]+ "\ n");         Interpreter.exec ("b=" +arg[1] + "\ n");         Interpreter.exec ("Print b/a");          Pass the address         String Txt_path = "\\python\\sample.txt";         Interpreter.exec ("Txt_path = \" "+ Txt_path +" \ "");         Interpreter.exec ("Print Txt_path");    } Main


Reference question and Answer: How topassarguments to Python script in Java using Jython


Jython:java How to pass a value to Python

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.