SlideShare a Scribd company logo
INTRODUCTION TO
JYTHON
John
Saturday, December 21, 2013
The reason for Jython
• Access Java class library
• Embed in Java app servers
• Mix and match Python and Java code
Install and configure Jython
• Download the jar file from
www.jython.org/downloads.html
• download the jar file (latest version is
jython.2.7 alpha,which means you will have
all same feature as Python 2.7)
• Add “C:jython2.7a2” into the path, the file
jython.bat is the executable file
Attention: if you met problems in
the interactive console
if you met this problem
• Code never end if input a
string

Use chcp solve it
Input command on line
$ chcp 437
import Java built-in classes
Use HashMap java class

covert HashMap to dict

>>> from java.util import
HashMap
>>> a = HashMap()
>>> a. get(“key”,1)
>>> a
{key=1}
>>> type(a)
<type ‘java.util.HashMap’>

>>> d = dict()
>>> d.update(a)
>>> print d
{u’key’:1}
More examples
Call java swing libaray
from javax.swing import *
frame = JFrame(“Hello,Jython”)
label = JLabel(“Hello
Jython!”,JLabel.CENTER)
frame.add(label)
frame.setDefaultCloseOperation(JFra
me.EXIT_ON_CLOSE)
frame.setSize(200,100)
frame.close()
Mix Java and Python
Step 1: write java class in java
file

Step 2: compile it and add it
to CLASSPATH

#file Foo.java
public class Foo
{
public String stuff(int x)
{
String buf = new
String(“Test from Java:”);
buf = buf +
Integer.toString(x);
return buf
}
}

#Compile the java code
#A Foo.class will be generated if java
code have no error
$ javac Foo.java
#Build the jar file Foo.jar
$ jar cvf Foo.jar Foo.class
#update the CLASSPATH on windows
$ set CLASSPATH = %CLASSPATH%
C:/Foo.jar;
#use echo view the change
$ echo %CLASSPATH%
Use Java class in Python
import the Foo class

Inherit the Foo class

>>> import Foo
>>> f = Foo()
>>> type(f)
<type ‘Foo’>
>>> f.stuff(123)
u’This is a test from Java:123’

>>> class Bar(Foo):
def blah(self,x):
print “python, about to
do Java”
print self.stuff(x)
print “And back to
Python”
>>> b = Bar()
>>> b.blah(123)
Python, about to do Java
This is a test from Java: 123
And back to Python

More Related Content

What's hot (20)

PDF
Inroduction to golang
Yoni Davidson
 
PDF
Bytecode manipulation with Javassist and ASM
ashleypuls
 
ODP
Ast transformation
Gagan Agrawal
 
PPT
Groovy AST Demystified
Andres Almiray
 
PDF
Introduction to Groovy runtime metaprogramming and AST transforms
Marcin Grzejszczak
 
DOCX
QTP Interview Questions and answers
Rita Singh
 
PPTX
Java For Automation
Abhijeet Dubey
 
ODP
Groovy AST Transformations
hendersk
 
PPTX
Java byte code & virtual machine
Laxman Puri
 
PDF
Kotlin - Better Java
Dariusz Lorenc
 
PDF
Using the Groovy Ecosystem for Rapid JVM Development
Schalk Cronjé
 
PDF
Qtp interview questions and answers
ITeLearn
 
PDF
Introduction to Kotlin for Java developer
Shuhei Shogen
 
PPTX
Integration Group - Robot Framework
OpenDaylight
 
PPSX
Introduction to java
Ajay Sharma
 
PPTX
Java introduction
The icfai university jaipur
 
PPTX
Java Bytecode For Discriminating Developers - GeeCON 2011
Anton Arhipov
 
PPT
Basic java part_ii
Khaled AlGhazaly
 
PDF
Connecting the Worlds of Java and Ruby with JRuby
Nick Sieger
 
ODP
Java 9 Features
NexThoughts Technologies
 
Inroduction to golang
Yoni Davidson
 
Bytecode manipulation with Javassist and ASM
ashleypuls
 
Ast transformation
Gagan Agrawal
 
Groovy AST Demystified
Andres Almiray
 
Introduction to Groovy runtime metaprogramming and AST transforms
Marcin Grzejszczak
 
QTP Interview Questions and answers
Rita Singh
 
Java For Automation
Abhijeet Dubey
 
Groovy AST Transformations
hendersk
 
Java byte code & virtual machine
Laxman Puri
 
Kotlin - Better Java
Dariusz Lorenc
 
Using the Groovy Ecosystem for Rapid JVM Development
Schalk Cronjé
 
Qtp interview questions and answers
ITeLearn
 
Introduction to Kotlin for Java developer
Shuhei Shogen
 
Integration Group - Robot Framework
OpenDaylight
 
Introduction to java
Ajay Sharma
 
Java introduction
The icfai university jaipur
 
Java Bytecode For Discriminating Developers - GeeCON 2011
Anton Arhipov
 
Basic java part_ii
Khaled AlGhazaly
 
Connecting the Worlds of Java and Ruby with JRuby
Nick Sieger
 
Java 9 Features
NexThoughts Technologies
 

Similar to Introduction to jython (20)

PDF
Jython-Introduction
Mythili Shankar
 
PDF
Jython
Vijay Shukla
 
PDF
Jython: Integrating Python and Java
Charles Anderson
 
PPTX
Integrating Java with python
archanab47
 
PDF
Jython
Robert Bachmann
 
PDF
Java Programming Fundamentals: Complete Guide for Beginners
Taranath Jaishy
 
PDF
Jython on Django
fwierzbicki
 
PPTX
Core java over view basics introduction by quontra solutions
QUONTRASOLUTIONS
 
PDF
Jython
jbrendel
 
PDF
Jython in workflow and rules engines
Vaclav Tunka
 
PPTX
python.pptx
RRamyaDevi
 
PPT
Groovy-Eclipse at Eclipsecon2010
guest9ff00
 
PPT
Groovy-Eclipse at Eclipsecon2010
guest9ff00
 
ODP
Django on Jython, PyCon 2009
Leonardo Soto
 
PPTX
503111_Java Technology_Chapter 1. Introduction to Java Development.pptx
PhienNguyenNgoc1
 
PPTX
Introduction to java
Java Lover
 
PPTX
Pi j1.1 what-is-java
mcollison
 
PPTX
Introduction to java
Sandeep Rawat
 
Jython-Introduction
Mythili Shankar
 
Jython
Vijay Shukla
 
Jython: Integrating Python and Java
Charles Anderson
 
Integrating Java with python
archanab47
 
Java Programming Fundamentals: Complete Guide for Beginners
Taranath Jaishy
 
Jython on Django
fwierzbicki
 
Core java over view basics introduction by quontra solutions
QUONTRASOLUTIONS
 
Jython
jbrendel
 
Jython in workflow and rules engines
Vaclav Tunka
 
python.pptx
RRamyaDevi
 
Groovy-Eclipse at Eclipsecon2010
guest9ff00
 
Groovy-Eclipse at Eclipsecon2010
guest9ff00
 
Django on Jython, PyCon 2009
Leonardo Soto
 
503111_Java Technology_Chapter 1. Introduction to Java Development.pptx
PhienNguyenNgoc1
 
Introduction to java
Java Lover
 
Pi j1.1 what-is-java
mcollison
 
Introduction to java
Sandeep Rawat
 
Ad

More from John(Qiang) Zhang (11)

PPTX
Git and github introduction
John(Qiang) Zhang
 
PPT
Python testing
John(Qiang) Zhang
 
PPT
Profiling in python
John(Qiang) Zhang
 
PPT
Introduction to cython
John(Qiang) Zhang
 
PPT
A useful tools in windows py2exe(optional)
John(Qiang) Zhang
 
PPT
Python advanced 3.the python std lib by example –data structures
John(Qiang) Zhang
 
PPT
Python advanced 3.the python std lib by example – system related modules
John(Qiang) Zhang
 
PPT
Python advanced 3.the python std lib by example – application building blocks
John(Qiang) Zhang
 
PPTX
Python advanced 2. regular expression in python
John(Qiang) Zhang
 
PPT
Python advanced 1.handle error, generator, decorator and decriptor
John(Qiang) Zhang
 
PPT
Python advanced 3.the python std lib by example – algorithm
John(Qiang) Zhang
 
Git and github introduction
John(Qiang) Zhang
 
Python testing
John(Qiang) Zhang
 
Profiling in python
John(Qiang) Zhang
 
Introduction to cython
John(Qiang) Zhang
 
A useful tools in windows py2exe(optional)
John(Qiang) Zhang
 
Python advanced 3.the python std lib by example –data structures
John(Qiang) Zhang
 
Python advanced 3.the python std lib by example – system related modules
John(Qiang) Zhang
 
Python advanced 3.the python std lib by example – application building blocks
John(Qiang) Zhang
 
Python advanced 2. regular expression in python
John(Qiang) Zhang
 
Python advanced 1.handle error, generator, decorator and decriptor
John(Qiang) Zhang
 
Python advanced 3.the python std lib by example – algorithm
John(Qiang) Zhang
 
Ad

Recently uploaded (20)

PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PDF
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
PDF
Advancing WebDriver BiDi support in WebKit
Igalia
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
DOCX
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PDF
Staying Human in a Machine- Accelerated World
Catalin Jora
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PPTX
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
Advancing WebDriver BiDi support in WebKit
Igalia
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
Staying Human in a Machine- Accelerated World
Catalin Jora
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
The Project Compass - GDG on Campus MSIT
dscmsitkol
 

Introduction to jython

  • 2. The reason for Jython • Access Java class library • Embed in Java app servers • Mix and match Python and Java code
  • 3. Install and configure Jython • Download the jar file from www.jython.org/downloads.html • download the jar file (latest version is jython.2.7 alpha,which means you will have all same feature as Python 2.7) • Add “C:jython2.7a2” into the path, the file jython.bat is the executable file
  • 4. Attention: if you met problems in the interactive console if you met this problem • Code never end if input a string Use chcp solve it Input command on line $ chcp 437
  • 5. import Java built-in classes Use HashMap java class covert HashMap to dict >>> from java.util import HashMap >>> a = HashMap() >>> a. get(“key”,1) >>> a {key=1} >>> type(a) <type ‘java.util.HashMap’> >>> d = dict() >>> d.update(a) >>> print d {u’key’:1}
  • 6. More examples Call java swing libaray from javax.swing import * frame = JFrame(“Hello,Jython”) label = JLabel(“Hello Jython!”,JLabel.CENTER) frame.add(label) frame.setDefaultCloseOperation(JFra me.EXIT_ON_CLOSE) frame.setSize(200,100) frame.close()
  • 7. Mix Java and Python Step 1: write java class in java file Step 2: compile it and add it to CLASSPATH #file Foo.java public class Foo { public String stuff(int x) { String buf = new String(“Test from Java:”); buf = buf + Integer.toString(x); return buf } } #Compile the java code #A Foo.class will be generated if java code have no error $ javac Foo.java #Build the jar file Foo.jar $ jar cvf Foo.jar Foo.class #update the CLASSPATH on windows $ set CLASSPATH = %CLASSPATH% C:/Foo.jar; #use echo view the change $ echo %CLASSPATH%
  • 8. Use Java class in Python import the Foo class Inherit the Foo class >>> import Foo >>> f = Foo() >>> type(f) <type ‘Foo’> >>> f.stuff(123) u’This is a test from Java:123’ >>> class Bar(Foo): def blah(self,x): print “python, about to do Java” print self.stuff(x) print “And back to Python” >>> b = Bar() >>> b.blah(123) Python, about to do Java This is a test from Java: 123 And back to Python