I have a python script - and I need to run a .jar from it.
What I need is to send an array as input to the Java code, and I want to receive an array as output.
How can I do that? Thank you all in advanced!
asked Mar 9, 2017 at 9:04
Bramat
1,0074 gold badges25 silver badges43 bronze badges
-
You could use Jython.Eric Duminil– Eric Duminil2017年03月09日 09:10:40 +00:00Commented Mar 9, 2017 at 9:10
-
And what have you tried so far? It is not like you are the first person ever that wants to call some external program from Python. How many seconds (?) did you spent on doing some prior research?GhostCat– GhostCat2017年03月09日 09:29:44 +00:00Commented Mar 9, 2017 at 9:29
-
1@GhostCat I spent more then an hour googling for an answer - I saw use in Popen and in subprocess.call, but the examples regarding sending and receiving parameters were vague - so I asked for specific help... thought this is the place to ask, but thank you for the Pre-Judging!Bramat– Bramat2017年03月09日 10:14:44 +00:00Commented Mar 9, 2017 at 10:14
2 Answers 2
Since a while, the os module is depreciated (but can still be used) and subprocess is the suggested method.
import subprocess
result = subprocess.check_output(["java -jar example.jar", str(your_array)], shell=True)
You can also use Popen, see for example python getoutput() equivalent in subprocess
answered Mar 9, 2017 at 9:12
Roelant
5,2195 gold badges43 silver badges77 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
Python 2.7
#!/usr/bin/env python2.7
import os
import subprocess
# Make a copy of the environment
env = dict(os.environ)
env['JAVA_OPTS'] = 'foo'
subprocess.call(['java', '-jar', 'temp.jar'], env=env)
Python 3
#!/usr/bin/env python3
from subprocess import check_output
result = check_output(['java', '-jar', 'temp.jar'], input=b'foo')
Comments
default