1

I want to execute Embedded python in Java.

Python code

#!/usr/bin/python

import sys

print 'Number of arguments:', len(sys.argv), 'arguments.'
print 'Argument List:', str(sys.argv)

Java Code

    StringWriter writer = new StringWriter(); // ouput will be stored here
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptContext context = new SimpleScriptContext();

    context.setWriter(writer); // configures output redirection
    ScriptEngine engine = manager.getEngineByName("python");
    
    engine.eval(new InputStreamReader(Main.class.getResourceAsStream("/py/a.py")), context);
    System.out.println(writer.toString());

current output

Number of arguments: 1 arguments.

Argument List: ['']

How can I pass parameters to this script in my code?


You run the script in terminal by $python a.py hellow worldd.

now if you want to execute a.py embedded in java, how you can pass the arguments hellow worldd?

2
  • When you use ScriptEngine, you're not running the script in the same way as you do from command line. Instead, it just sees it as a stream that contains python commands. It is not a process forked by linux and passed an argv array. Therefore you can't use sys.argv in it. Commented Jul 7, 2015 at 10:03
  • @RealSkeptic is it possible use binding to define sys.argv variables before the execution of the stream? Commented Jul 7, 2015 at 14:26

1 Answer 1

-1

Parsing parameters to a script? I'm not sure what you mean by that. Do you mean pass a java object called argv, or do you mean replace a string in your script with another? Typically we talk about passing instances to the interpretor/script engine not the script, the script is just a txt file containing code, you can pass in java objects like this.

public class ExampleEmbeddingJython
{

    public static class Arg {
        int a = 3;

        public Arg(int a)
        {
            this.a = a;
        }

        public String toString()
        {
            return "a = " + a;
        }

        public int getA()
        {
            return a;
        }
    }

    public static void main(String[] args) throws PyException
    {
        PythonInterpreter interp = new PythonInterpreter();
        interp.set("arg", new Arg(42));
        interp.exec("print arg");
        interp.exec("x = arg.getA()+2");
        PyObject x = interp.get("x");
        System.out.println("x = " + x);
        System.out.println("Goodbye, cruel world");
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

passing, not parsing. I mean: suppose you have a script that you run it in terminal by $python a.py --proxy 127.0.0.1:8080 --verbose. now if you want to execute a.py embedded in java, how you can pass the arguments --proxy 127.0.0.1:8080 --verbose?
YOU DON'T PARSE/PASS/PAS arguments to a script!!!!!!!!!!! please stop saying that, your pass arguments to python.exe on the command line just like any other .exe, they are accessible in sys.argv, now that you have embedded python in java there is no python.exe, only the java.exe. You can pass arguments to java acessible from the main(String[] args) then give a string arguement to interpreter like interp.set("arg", args); Or interp.set("-proxy 127.0.0.1:8080 --verbose", args); come on read the answer and then spend 10 seconds thinking you will get it.
I'm not using PythonInterpreter class. I'm using ScriptEngine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.