Wafula Samuel
Wafula Samuel

Reputation: 539

How to use jython standalone jar file in a Java application

I'm creating a Java application and will best work if I incorporate Python in it. I'm having jython-standalone-2.7.0.jar lib. My question is, how do I use this lib like create a simple hello world application written in Python and display the output in java? I have Python 3.5 installed. Do I need it too or is there anything else I need? Please help, thanks.

Upvotes: 2

Views: 3574

Answers (1)

zonk
zonk

Reputation: 167

if you're still interested in an answer:

0) Jython isn't supporting Python 3 (so far), only Python 2.7.

1) Create a Python file with a function f.e.:

#hello.py
def printhello():
    print("Hello World from hello.py")

2) Unzip your jython-standalone-2.7.0.jar and add your "hello.py" file into the folder "Lib".

3) Close your jython-standalone-2.7.0.jar again

4) Create your "main file":

#main.py
import hello
print("Hello from main file")
hello.printhello()
print("I'm back in main")

And add it into the same file where your standalone.jar is.

5) In your terminal/cmd execute: java -jar jython-standalone-2.7.0.jar main.py

6) Success (I hope ;-) )

P.S. If you're using Windows, don't use the OS own zip methods, because in my case my jar file was rubbish afterwards.

Upvotes: 1

Related Questions