Skip to content Skip to sidebar Skip to footer

Passing Arguments To Python Script In Java

I'm running a python script in a java class like this: PythonInterpreter interp = new PythonInterpreter(); PythonInterpreter.initialize(System.getProperties(), System.getProperties

Solution 1:

Last argument in your below call is for command line arguments:

PythonInterpreter.initialize(System.getProperties(), System.getProperties(), new String[0]);

From PythronInterpreter javadocs:

initialize

public static void initialize(Properties preProperties, Properties postProperties, String[] argv)

Initializes the Jython runtime. This should only be called once, before any other Python objects (including PythonInterpreter) are created. Parameters: preProperties - A set of properties. Typically System.getProperties() is used. preProperties override properties from the registry file. postProperties - Another set of properties. Values like python.home, python.path and all other values from the registry files can be added to this property set. postProperties override system properties and registry properties. argv - Command line arguments, assigned to sys.argv.

Solution 2:

I had the same issue and found it could be resolved by using "interned" string, i.e.,

for (int i = 0; i args.length; ++i) {
    args[i] = args[i].intern();
}

I am using Jython 2.5.3. Hope this will help.

Post a Comment for "Passing Arguments To Python Script In Java"