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!

asked Mar 9, 2017 at 9:04
3
  • You could use Jython. Commented 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? Commented 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! Commented Mar 9, 2017 at 10:14

2 Answers 2

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

answered Mar 9, 2017 at 9:12
Sign up to request clarification or add additional context in comments.

Comments

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
1,1681 gold badge16 silver badges24 bronze badges
answered Mar 9, 2017 at 9:13

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.