0

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!

Bramat
  • 979
  • 4
  • 24
  • 40
  • You could use [Jython](http://www.jython.org/Project/). – Eric Duminil Mar 09 '17 at 09: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 Mar 09 '17 at 09: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 Mar 09 '17 at 10:14

2 Answers2

1

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

Community
  • 1
  • 1
Roelant
  • 4,508
  • 1
  • 32
  • 62
0

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')
matan h
  • 900
  • 1
  • 10
  • 19
osanger
  • 2,276
  • 3
  • 28
  • 35