Error passing arguments from Java to Python -
i'm calling on python modules java. module using numpy, i'm using runtime.getruntime().exec()
call python. in java doing this:
file python = new file("/usr/bin/python2.7"); file script = new file("/opt/my_custom_script.py"); string[] cmdarray = new string[] { python.tostring(), script.tostring(), "-i infile.txt", "-o outfile.txt" }; process p = runtime.getruntime().exec(cmdarray); p.waitfor(); if (p.exitvalue() != 0) { throw new exception(scriptname + " failed exit code " + p.exitvalue()); }
and in python i've gotten far:
def main(argv): try: opts, args = getopt.getopt(argv, "i:o:") except getopt.getopterror err: print(err) sys.exit(128) # made number know when happens sys.exit(0) if __name__ == "__main__": main(sys.argv[1:])
every time run keep on getting error number made up, , python script runs no further. calling on python directly (in bash) gives me no problems.
where disconnect? how go troubleshooting/debugging this?
your problem passing 2 options 2 script rather 4 getopt expects. is, -i infile.txt
treated 1 option, not, getopt expects, 2 options -i
, infile.txt
, , same thing happening -o outfile.txt
. can fix replacing line:
string[] cmdarray = new string[] { python.tostring(), script.tostring(), "-i infile.txt", "-o outfile.txt" };
with line:
string[] cmdarray = new string[] { python.tostring(), script.tostring(), "-i", "infile.txt", "-o", "outfile.txt" };
notice -i
, infile.txt
separate array elements, -o
, -outfile.txt
.
Comments
Post a Comment