Top Related Projects
Quick Overview
Jython is an implementation of the Python programming language designed to run on the Java platform. It allows Python code to be compiled to Java bytecode and run on a Java Virtual Machine (JVM), enabling seamless integration between Python and Java.
Pros
- Seamless integration with Java libraries and frameworks
- Ability to leverage the performance and scalability of the JVM
- Access to Java's multithreading capabilities
- Easy deployment on Java application servers
Cons
- Slower execution compared to CPython for certain operations
- Limited support for some Python C extensions
- Lags behind CPython in terms of version updates
- Smaller community and ecosystem compared to CPython
Code Examples
- Importing and using Java classes:
from java.util import ArrayList
list = ArrayList()
list.add("Hello")
list.add("World")
print(list)
- Creating a Java-style GUI using Swing:
from javax.swing import JFrame, JLabel
frame = JFrame("Hello Jython")
label = JLabel("Welcome to Jython!")
frame.add(label)
frame.setSize(300, 100)
frame.setVisible(True)
- Using Java's multithreading capabilities:
from java.lang import Runnable, Thread
class MyRunnable(Runnable):
def run(self):
print("Hello from a thread!")
thread = Thread(MyRunnable())
thread.start()
Getting Started
- Download Jython installer from the official website: https://www.jython.org/download
- Run the installer and follow the installation wizard
- Add Jython to your system PATH
- Create a new file with a
.py
extension - Run your Jython script using the command:
jython your_script.py
To use Java classes in your Jython script, ensure that the necessary Java libraries are in your classpath when running Jython.
Competitor Comparisons
JPype is cross language bridge to allow Python programs full access to Java class libraries.
Pros of JPype
- Allows direct access to Java classes and objects from Python
- Supports bidirectional callbacks between Python and Java
- Enables use of Java libraries without rewriting Python code
Cons of JPype
- Requires Java Virtual Machine (JVM) to be running
- May have performance overhead due to JNI calls
- Limited support for some advanced Java features
Code Comparison
JPype:
import jpype
jpype.startJVM()
java_string = jpype.java.lang.String("Hello, Java!")
print(java_string.toUpperCase())
jpype.shutdownJVM()
Jython:
from java.lang import String
java_string = String("Hello, Java!")
print(java_string.toUpperCase())
Key Differences
- JPype allows mixing Python and Java in the same process, while Jython is a complete Python implementation in Java
- Jython provides seamless integration with Java, but requires writing Python code compatible with Jython
- JPype maintains Python's native syntax and libraries, while Jython may have limitations with certain Python packages
Use Cases
- JPype: Ideal for projects requiring specific Java libraries while maintaining Python as the primary language
- Jython: Better suited for Java-centric environments or when full Java integration is necessary
Embed Python in Java
Pros of Jep
- Faster execution for Python code embedded in Java
- Easier integration with existing Java projects
- More lightweight and focused on Java-Python interoperability
Cons of Jep
- Limited support for Python standard library modules
- Less comprehensive implementation of Python language features
- Requires native libraries, which can complicate deployment
Code Comparison
Jython:
from java.util import ArrayList
list = ArrayList()
list.add("Hello")
list.add("World")
print(list)
Jep:
import jep.Jep;
Jep jep = new Jep();
jep.eval("import sys");
jep.eval("print(sys.version)");
jep.close();
Summary
Jep focuses on embedding Python in Java applications, offering better performance and easier integration with existing Java projects. However, it has limitations in Python language support and standard library availability. Jython provides a more complete Python implementation but may be slower and heavier. The choice between them depends on specific project requirements and constraints.
Access Java classes from Python
Pros of Pyjnius
- Lightweight and focused on Java/Python interoperability
- Easier to integrate into existing Python projects
- More actively maintained with recent updates
Cons of Pyjnius
- Limited scope compared to Jython's full Python implementation
- Requires more manual work for complex Java interactions
- Less comprehensive documentation and community support
Code Comparison
Jython example:
from java.util import ArrayList
list = ArrayList()
list.add("Hello")
list.add("World")
print list.get(0) + " " + list.get(1)
Pyjnius example:
from jnius import autoclass
ArrayList = autoclass('java.util.ArrayList')
list = ArrayList()
list.add("Hello")
list.add("World")
print(list.get(0) + " " + list.get(1))
Both Jython and Pyjnius aim to bridge Python and Java, but they take different approaches. Jython provides a full Python implementation running on the JVM, allowing seamless integration with Java libraries and classes. Pyjnius, on the other hand, focuses on providing a lightweight way to call Java methods from Python, making it easier to use in existing Python projects.
Jython offers a more comprehensive solution for running Python on the JVM, while Pyjnius provides a simpler, more focused approach for Java/Python interoperability. The choice between them depends on the specific requirements of your project and the level of Java integration needed.
JRuby, an implementation of Ruby on the JVM
Pros of JRuby
- Better performance and JIT compilation
- More active development and frequent updates
- Seamless Java interoperability
Cons of JRuby
- Larger memory footprint
- Slower startup time
- Some Ruby gems may not be compatible
Code Comparison
JRuby example:
require 'java'
java_import 'java.util.ArrayList'
list = ArrayList.new
list.add("Hello")
list.add("World")
puts list.to_s
Jython example:
from java.util import ArrayList
list = ArrayList()
list.add("Hello")
list.add("World")
print(list)
Both Jython and JRuby are implementations of dynamic languages (Python and Ruby, respectively) that run on the Java Virtual Machine (JVM). They allow developers to leverage the extensive Java ecosystem while using their preferred language syntax.
JRuby tends to have better performance due to its more advanced JIT compilation techniques, while Jython may have a slight edge in startup time and memory usage. JRuby has seen more active development in recent years, resulting in better compatibility with newer Ruby versions.
Both projects aim to provide seamless integration with Java libraries, but JRuby's approach to Java interoperability is often considered more intuitive for Ruby developers.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Jython: Python for the Java Platform
This is the development repository of Jython, the implementation of Python in Java. Only version 2.7 of Python can be supported at present (but watch this space for a 3.x version).
Compatibility
Jython provides good compatibility with Python 2.7 the language. Also, a high proportion of the standard library is included, taken from late versions of CPython (around 2.7.13). Some standard library modules have a Jython-specific implementation that has not kept pace with its CPython counterpart.
Jython 2.7 support for the Python ecosystem
includes built-in support of pip/setuptools.
You can use bin/pip
if the targets do not include C
extensions.
There is a native launcher for Windows (bin/jython.exe
)
that works essentially like the python
command.
Jim Baker presented a talk at PyCon 2015 about Jython 2.7, including demos of new features: https://www.youtube.com/watch?v=hLm3garVQFo
Support
Python 2.7 (the language) is no longer supported by the PSF. Running on Jython should not be considered an alternative to porting your application to Python 3, due to the limitations cited here and the small amount of effort available to support 2.7.x. Jython 2.7 is offered for continuity because a 3.x is not yet available.
See ACKNOWLEDGMENTS for details about Jython's copyright, license, contributors, and mailing lists. Consult NEWS for detailed release notes, including bugs fixed, backward breaking changes, and new features. We sincerely thank all who contribute to Jython, by bug reports, patches, pull requests, documentation changes and e-mail discussions.
Downloads
Binary downloads are available from https://www.jython.org/download along with Maven and Gradle dependency information.
How to build Jython
The project uses Git for version-control, and the master repository is at https://github.com/jython/jython, You should clone this repository to create a buildable copy of the latest state of the Jython source. Start a new branch for any bug-fix or experimentation you plan.
The previously authoritative repository at https://hg.python.org/jython is not now in use, remaining frozen at v2.7.2.
Build using ant
for development
Jython is normally built using ant
.
It is necessary to have Ant and at least a Java 8 SDK on the path.
To build Jython in development, we generally use the command:
ant
This leaves an executable in dist/bin
that you may run from the check-out root with:
dist/bin/jython
Other ant
targets exist, notably clean
, javatest
and jar
.
You can test your build of Jython (by running the regression tests), with the command:
dist/bin/jython -m test.regrtest -e -m regrtest_memo.txt
or by invoking the Ant target regrtest
.
Build an installer using ant
If you want to install a snapshot build of Jython, use the command:
ant installer
This will leave you with a snapshot installer JAR in dist
,
that you can run with:
java -jar jython-installer.jar
for the graphical installer, or:
java -jar jython-installer.jar --console
For the console version. (A --help
option gives you the full story.)
Build a JAR using Gradle
We have a Gradle build that results in a family of JARs and a POM. This is intended to provide the Jython core in a form that Gradle and Maven users can consume as a dependency. Invoke this with:
PS> .\gradlew publish
and a JAR and POM are delivered to .build2\repo
Whereas the JARs delivered by the installer are somewhat "fat", embedding certain dependencies in shaded (renamed) form, the JAR from the Gradle build is "spare" and cites its dependencies externally through a POM.
Top Related Projects
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot