pyjnius "Class not found" when importing jar file
Asked Answered
N

7

6

I'm trying to make pyjnius work with a jar file I built from java application, but I keep getting the "Class not found" error:

>>> import os
>>> os.environ['CLASSPATH'] = "~/workspace/myapp-Tools/Admin/Console/couchdb/myapp-web.jar"
>>> from jnius import autoclass
>>> bla = autoclass('com/myapp/webapp/server/helpers/licensee/CalculationHelper')
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 File "/home/sam/workspace/myapp-Tools/Admin/Console/couchdb/virtualenv/local/lib/python2.7/site-packages/jnius/reflect.py", line 150, in autoclass
   c = find_javaclass(clsname)
 File "jnius_export_func.pxi", line 23, in jnius.find_javaclass (jnius/jnius.c:12815)
jnius.JavaException: Class not found 'com/myapp/webapp/server/helpers/licensee/CalculationHelper'
>>> 

of course I've checked:

jar tf myapp-web.jar

and com/myapp/webapp/server/helpers/licensee/CalculationHelper.class is in there

I've also tried setting the classpath this way:

import jnius_config
jnius_config.set_classpath('.', '~/workspace/myapp-Tools/Admin/Console/couchdb/')
#import jnius
from jnius import autoclass

But this gave me the same result. I'm working on a virtualenv btw. I've tried all approaches I could find online, but it is simply not working. I had to manually install pyjnius because using pip got me an old version of it. Any help would be greatly appreciated.


Edit: tried this with a jar not created by me and I see a different error

Python 2.7.6 (default, Mar 22 2014, 22:59:56) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import jnius_config
>>> jnius_config.add_classpath('/home/sam/workspace/someproject/*')
>>> jnius_config.expand_classpath()
'/home/sam/workspace/someproject/annotations.jar:/home/sam/workspace/someproject/junit-4.10.jar:/home/sam/workspace/someproject/    postgresql-8.1-408.jdbc3.jar'
>>> import jnius
>>> from jnius import autoclass
>>> test = autoclass('org/postgresql/geometric/PGcircle.class')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/sam/workspace/myapp-Tools/Admin/Console/couchdb/virtualenv/local/lib/python2.7/site-packages/jnius/reflect.py", line 150,     in autoclass
    c = find_javaclass(clsname)
  File "jnius_export_func.pxi", line 23, in jnius.find_javaclass (jnius/jnius.c:12815)
jnius.JavaException: Class not found 'org/postgresql/geometric/PGcircle/class'
>>> test = autoclass('org/postgresql/geometric/PGcircle')
Exception in thread "main" java.lang.NoClassDefFoundError: org/postgresql/geometric/PGcircle/class
Caused by: java.lang.ClassNotFoundException: org.postgresql.geometric.PGcircle.class
  at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
  at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
  at java.security.AccessController.doPrivileged(Native Method)
  at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
  at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/sam/workspace/myapp-Tools/Admin/Console/couchdb/virtualenv/local/lib/python2.7/site-packages/jnius/reflect.py", line 156,     in autoclass
    for constructor in c.getConstructors():
  File "jnius_export_class.pxi", line 562, in jnius.JavaMethod.__call__ (jnius/jnius.c:19385)
  File "jnius_export_class.pxi", line 649, in jnius.JavaMethod.call_method (jnius/jnius.c:20409)
  File "jnius_utils.pxi", line 43, in jnius.check_exception (jnius/jnius.c:3533)
jnius.JavaException: JVM exception occured
>>> test = autoclass('org/postgresql/geometric/PGcircl')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/sam/workspace/myapp-Tools/Admin/Console/couchdb/virtualenv/local/lib/python2.7/site-packages/jnius/reflect.py", line 150,     in autoclass
    c = find_javaclass(clsname)
  File "jnius_export_func.pxi", line 23, in jnius.find_javaclass (jnius/jnius.c:12815)
jnius.JavaException: Class not found 'org/postgresql/geometric/PGcircl'
>>> 

and here are the contents of jar tf on that jar:

sam@lambda ~/workspace$ jar tf ./someproject/postgresql-8.1-408.jdbc3.jar
META-INF/
META-INF/MANIFEST.MF
...
org/postgresql/geometric/PGbox.class
org/postgresql/geometric/PGcircle.class
org/postgresql/geometric/PGline.class
org/postgresql/geometric/PGlseg.class
org/postgresql/geometric/PGpath.class
org/postgresql/geometric/PGpoint.class
org/postgresql/geometric/PGpolygon.class
...
sam@lambda ~/workspace$ 

Again... any help will be greatly appreciated!

Nyaya answered 15/1, 2015 at 21:34 Comment(0)
K
5

tl;dr: make sure the .java files are compiled to (at most) the same Java version .class files as the Java version on the system that will import the file with pyjnius.

Longer version:

I had a very similar problem, with one big difference: some files worked without any problem and others (in the same directory) didn't.

The problem with the files that resulted in the 'Class not found' exception was that I compiled them under Windows, which has Java 8. Ubuntu however currently installs Java 7 when you run "sudo apt-get install default-jdk".

And so, pyjnius couldn't import the Java 8 files on my Java 7 Ubuntu install. It's strange that it throws a 'Class not found' exception, instead of something more descriptive. Changing the target output to 1.7 fixed my problem.

Kristynkrock answered 21/10, 2015 at 12:32 Comment(1)
+1 It's strange that it throws a 'Class not found' exception, instead of something more descriptive - This points out the general issue, which is seemingly any runtime issue with the jar may be the root cause of this issue. For those struggling, verify you can run the jar standalone in the same environment you're python application is trying to run it from.Whippet
M
2

I solved this problem by exporting the JAR as a runnable JAR file in Eclipse:

  1. create an empty main method somewhere if you don't have one (export didn't work for me otherwise)
  2. go to File->Export...
  3. select Java->Runnable JAR file
  4. click Next
  5. select the main method in the Launch configuration
  6. select your Export destination
  7. select "Copy required libraries into a sub-folder next to the generated JAR" as Library handling (the only option that worked for me in my special case, but you can also test the others)
  8. click Finish
Madness answered 14/9, 2015 at 12:50 Comment(0)
H
1

It works fine when I use my jar file. Did you try to use the full path to define CLASSPATH?

  • Windows 7
  • Python 2.7.8
  • jnius 1.1-dev

This page will be useful as a reference. http://www.hackzine.org/using-apache-tika-from-python-with-jnius.html

I just wanted to leave a comment but I don't have enough reputation for it. So I leave an answer as a comment.

And you'd better not to use "/" instead of "." when you call autoclass. See the link below.

http://pyjnius.readthedocs.org/en/latest/api.html#jnius.autoclass

Higherup answered 27/1, 2015 at 3:39 Comment(2)
Thanks pgkireek, I'm using Ubuntu 14.04.1 LTS, Python 2.7.6 and pyjnius directly from github.com/kivy/pyjnius. I had read both links you sent but none were of help in my case.Nyaya
And about using "/" instead of "." when calling autoclass, I've tried both with no success.. =/Nyaya
F
1

Old Post, but posting an answer if it is useful for someone.

I see two issues there, not sure if you have tried these together:

Change

jnius_config.set_classpath('.', '~/workspace/myapp-Tools/Admin/Console/couchdb/')

To

jnius_config.set_classpath('.', '~/workspace/myapp-Tools/Admin/Console/couchdb/*')

And

test = autoclass('org/postgresql/geometric/PGcircl')

To

test = autoclass('org.postgresql.geometric.PGcircl')

Freebooter answered 1/11, 2018 at 13:55 Comment(0)
B
0

Have you tried to add CLASSPATH via export then run your python script? This worked for me.

$ export CLASSPATH="~/workspace/myapp-Tools/Admin/Console/couchdb/myapp-web.jar"
Bandeau answered 3/2, 2015 at 10:14 Comment(3)
Hi Benxy yes, I tried it with no luck. Both with relative- and absolute path.Nyaya
But did you actually tried to set env var from shell? Not from python script. In my project this: import os os.environ['CLASSPATH'] = "/home/code/my_proj/my.jar" didn't work. First I exported CLASSPATH="/home/code/my_proj/my.jar" in shell and then ran "python my_script.py" and then autoclass worked. When I set the env var from shell it worked like a charm. Chect out this link: askubuntu.com/questions/389683/…Bandeau
Hi Benxy, again Yes. I built a wrapper script in bash to call the python script setting the classpath before calling the python script. ThanksNyaya
M
0

i solved this isue by putting CLASSPATH in .bashrc

CLASSPATH="~/documents/download/programs/tika-app.jar"

and it works properly

Mariselamarish answered 18/3, 2019 at 1:13 Comment(0)
C
0

The code below adds a jar to the classpath and then displays the classpath to further debug.

import os
import jnius_config

jnius_config.add_classpath("PATH_HERE/SOME.jar")

from jnius import autoclass, cast

ClassLoader = autoclass('java.lang.ClassLoader')
cl = ClassLoader.getSystemClassLoader()
ucl = cast('java.net.URLClassLoader', cl)
urls = ucl.getURLs()
tmp = [url.getFile() for url in urls]
print('\n'.join(tmp))
Cartercarteret answered 19/10, 2020 at 22:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.