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?
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.sys.argvvariables before the execution of the stream?