Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pyjnius import jar file

Pyjnius allows you to create a python wrapper for java classes like:

Hardware = autoclass('org.myapp.Hardware')

Is there a way to import an existing *.jar file like that? What does the syntax look like?

like image 562
eviltnan Avatar asked Feb 02 '14 02:02

eviltnan


People also ask

What is Pyjnius?

Pyjnius is a Python library for accessing Java classes. It either starts a new JVM inside the process, or retrieves the already surrounding JVM (for example on Android). This documentation is divided into differents parts. We recommend you to start with Installation, and then head over. to the Quickstart.


2 Answers

You can add the jar into CLASSPATH, then import pyjnius and use autoclass as normal :-):

import os
os.environ['CLASSPATH'] = "path/to/your.jar"

from jnius import autoclass


Bla = autoclass('bla.bla.BlaClass')
like image 66
imcaspar Avatar answered Sep 22 '22 18:09

imcaspar


As much as jar file relevant only for android, you need to add jar file in your buildozer.spec like

android.add_jars = java/myjar.jar

and in your App's build method

from kivy.utils import platform
...
if platform() == 'android':
    BlaClass = autoclass('java.bla.BlaClass')
...
like image 38
eviltnan Avatar answered Sep 21 '22 18:09

eviltnan