Pyjnius import jar file
Asked Answered
M

2

8

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?

Macintosh answered 2/2, 2014 at 2:26 Comment(0)
M
2

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')
...
Macintosh answered 2/2, 2014 at 22:5 Comment(2)
I just want to use some classes provided in some external jars in my python script. Is it not possible using jars which are not related to android?Lucio
it depends on what you want to achieve. maybe jython would work for youMacintosh
M
11

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')
Mcchesney answered 9/1, 2015 at 12:23 Comment(3)
What if i have more than 1 jar file , how i can include them ?Timothytimour
by separating them with : I would assumeAm‚lie
A slightly cleaner equivalent would be import jnius_config; jnius_config.set_classpath('/path/to/your.jar')Leboff
M
2

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')
...
Macintosh answered 2/2, 2014 at 22:5 Comment(2)
I just want to use some classes provided in some external jars in my python script. Is it not possible using jars which are not related to android?Lucio
it depends on what you want to achieve. maybe jython would work for youMacintosh

© 2022 - 2024 — McMap. All rights reserved.