pyjnius/docs/source/quickstart.rst

55 lines
1.5 KiB
ReStructuredText
Raw Normal View History

2012-08-16 10:56:29 +00:00
.. _quickstart:
Quickstart
==========
2012-08-16 12:07:52 +00:00
Eager to get started? This page will give you a good introduction to Pyjnius. It assumes
2012-08-16 10:56:29 +00:00
you have already Pyjnius installed. If you do not, head over the
:ref:`installation` section.
A minimal example
-----------------
A minimal Pyjnius example looks something like this::
from jnius import autoclass
2012-08-16 10:56:29 +00:00
Stack = autoclass('java.util.Stack')
stack = Stack()
stack.push('hello')
stack.push('world')
print stack.pop() # --> 'world'
print stack.pop() # --> 'hello'
Just save it as `test.py` (or something similar) and run it with your Python
2012-08-16 12:07:52 +00:00
interpreter. Make sure not to call your application `jnius.py` because it would
2012-08-16 10:56:29 +00:00
conflict with Pyjnius itself::
$ python test.py
world
hello
Automatic recursive inspection
------------------------------
2012-08-16 12:07:52 +00:00
Pyjnius uses Java reflection to give you a new autoclass() if the return type is
2012-08-16 10:56:29 +00:00
not a native type. Let's see this example::
2012-08-17 23:28:25 +00:00
System = autoclass('java.lang.System')
2012-08-16 10:56:29 +00:00
System.out.println('Hello World')
We only declared the first System class, but we are able to use all the static
fields and methods naturally. Let's go deeper::
2012-08-17 23:28:25 +00:00
>>> System = autoclass('java.lang.System')
2012-08-16 10:56:29 +00:00
>>> System
<class 'jnius.java.lang.System'>
2012-08-16 10:56:29 +00:00
>>> System.out
<java.io.PrintStream at 0x234df50 jclass=java/io/PrintStream jself=37921360>
>>> System.out.println
<jnius.JavaMethodMultiple object at 0x236adb8>
2012-08-16 12:07:52 +00:00
The recursive reflection always gives you an appropriate object that reflects the
2012-08-16 10:56:29 +00:00
returned Java object.