Author Topic: JPype: use Java like a boss  (Read 541 times)

Pinako

  • Hero Member
  • *****
  • Posts: 1263
    • View Profile
    • inportb
JPype: use Java like a boss
« on: May 06, 2011, 11:28:55 PM »
I like Python, but I also like some Java libraries. What's an amateur (such as me) to do short of hopping on the Jython bandwagon? Embed the JVM in Python, that's what ;)

# apt-get install python-jpype
Code: [Select]
#!/usr/bin/env python
from jpype import *
startJVM('/usr/lib/jvm/java-6-openjdk/jre/lib/i386/server/libjvm.so','-ea')
java.lang.System.out.println('Hello World')
shutdownJVM()
$ ./hello.py
Hello World
JVM activity report     :
        classes loaded       : 30
JVM has been shutdown

It does add a few megs to my Python process and JNI is not exactly optimal, but this is a nice way to use Java features in Python without sacrificing Python functionality. I plan to use this the same way I work with Cython: write most of the code in Python and bits in Java when it would be beneficial.

zzbomb

  • Hero Member
  • *****
  • Posts: 963
  • The awesome server admin
    • View Profile
    • ChemicalServers
Re: JPype: use Java like a boss
« Reply #1 on: May 06, 2011, 11:31:46 PM »
Wow. Seriously...? ok cool.