27.08.2019 Views

Jython(Java and Python Intergration)

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Jython</strong>(<strong>Java</strong> <strong>and</strong> <strong>Python</strong> Integration)<br />

- Ramireddy A


Ramireddy A<br />

Introduction :<br />

‣ <strong>Jython</strong> is the JVM implementation of the <strong>Python</strong> programming language. It is designed to<br />

run on the <strong>Java</strong> platform.<br />

‣ <strong>Jython</strong> was created in 1997 by Jim Hugunin<br />

‣ <strong>Jython</strong> 2.7.0 was released in May 2015<br />

‣ <strong>Jython</strong> jar file : https://jar-download.com/artifacts/org.python/jython-st<strong>and</strong>alone/2.7.1


Difference between <strong>Python</strong> <strong>and</strong> <strong>Java</strong>:<br />

Ramireddy A<br />

<strong>Python</strong> is a dynamically typed language. Hence, the type declaration of variable is<br />

not needed. <strong>Java</strong> on the other h<strong>and</strong> is a statically typed language, which means<br />

that the type declaration of variable is m<strong>and</strong>atory <strong>and</strong> cannot be changed.<br />

<strong>Python</strong> has only unchecked exceptions, whereas <strong>Java</strong> has both checked <strong>and</strong><br />

unchecked exceptions.<br />

<strong>Python</strong> uses indents for scoping, while <strong>Java</strong> uses matching curly brackets.<br />

Since <strong>Python</strong> is an interpreter-based language, it has no separate compilation steps.<br />

A <strong>Java</strong> program however needs to be compiled to bytecode <strong>and</strong> is in turn executed<br />

by a JVM.<br />

<strong>Python</strong> supports multiple inheritance, but in <strong>Java</strong>, multiple inheritance is not possible.<br />

It however has implementation of an interface.<br />

Compared to <strong>Java</strong>, <strong>Python</strong> has a richer built-in data structures (lists, dicts, tuples,<br />

everything is an object).


Difference between <strong>Python</strong> <strong>and</strong> <strong>Jython</strong><br />

Reference implementation of <strong>Python</strong>, called C<strong>Python</strong>, is written in C language.<br />

<strong>Jython</strong> on the other h<strong>and</strong> is completely written in <strong>Java</strong> <strong>and</strong> is a JVM implementation.<br />

St<strong>and</strong>ard <strong>Python</strong> is available on multiple platforms. <strong>Jython</strong> is available for any<br />

platform with a JVM installed on it.<br />

St<strong>and</strong>ard <strong>Python</strong> code compiles to a .pyc file, while <strong>Jython</strong> program compiles to<br />

a .classfile.<br />

<strong>Python</strong> extensions can be written in C language. Extensions for <strong>Jython</strong> are written in<br />

<strong>Java</strong>.<br />

<strong>Jython</strong> is truly multi-threaded in nature. <strong>Python</strong> however uses the Global Interpreter<br />

Lock (GIL) mechanism for the purpose.<br />

Both implementations have different garbage collection mechanisms.<br />

Ramireddy A


How to Install <strong>Jython</strong>:<br />

Ramireddy A<br />

Software link : https://www.jython.org/downloads.html<br />

Syntax : java -jar jython_installer-2.7.0.jar


<strong>Python</strong>Interpreter :<br />

Ramireddy A<br />

‣ Download jython-st<strong>and</strong>alone-2.7.0.jar - For embedding <strong>Jython</strong> in <strong>Java</strong> applications<br />

from their official downloads page: http://www.jython.org/downloads.html <strong>and</strong> include<br />

this jar file in <strong>Java</strong> CLASSPATH environment variable.<br />

‣ This library contains the <strong>Python</strong>Interpreter class.<br />

‣ Using the object of this class, any <strong>Python</strong> script can be executed using<br />

the execfile() method. The <strong>Python</strong>Interpreter enables you to make use<br />

of PyObjects directly.


<strong>Python</strong> Interpreter methods :<br />

Ramireddy A<br />

1. setIn(PyObject) 2. setIn(java.io.Reader)<br />

3. setIn(java.io.InputStream) 4. setOut(PyObject)<br />

5. setOut(java.io.Writer) 6. setOut(java,io.OutputStream)<br />

7. setErr(PyObject) 8. setErr(java.io.Writer<br />

9. setErr(java.io.OutputStream) 10. eval(String)<br />

11. eval(PyObject) 12. exec(String)<br />

13. exec(PyObject) 14. execfile(String filename)<br />

15. execfile(java.io.InputStream) 16. compile(String)<br />

17. compile(script, filename) 18.set(String name, Object value)<br />

19. set(String name, PyObject value) 20. get(String)<br />

21. get(String name, Classjavaclass)


Project Directory:<br />

Ramireddy A


Sample – 1:<br />

Ramireddy A<br />

<strong>Java</strong> Program<br />

<strong>Python</strong> file


<strong>Java</strong> Program<br />

Sample – 2 :<br />

<strong>Python</strong> file<br />

Ramireddy A


Thank Q<br />

Ramireddy A

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!