Python Jpype can't see Java classes in JAR file

1.3k views Asked by At

I am trying to work through what should be a simple problem, but am missing something. I have a very simple Java class:

package blah.blah;

public class Tester {
    public String testMethod1() { return "GotHere"; }
    public String testMethod2() { return "GotHereToo"; }
} // Tester

I am trying to get it to load into JPype:

import jpype
import jpype.imports
from jpype.types import *

path_to_jvm = "C:\\Java\\OracleJDK-8_241_x64\\jre\\bin\\server\\jvm.dll"
jpype.startJVM(path_to_jvm, classpath=["C:\\Users\\Administrator\\eclipse-workspace\\JpypeTest\\src;"])

from java.lang import System
print(System.getProperty("java.class.path"))

from java.io import ObjectInputStream
from blah.blah import Tester

tester = Tester()
print(tester.testMethod1())
print(tester.testMethod2())

jpype.shutdownJVM()

Everything works fine when I load the class directly using the above code. It stops working when I try to get it to load the Class file via a JAR.

jpype.startJVM(path_to_jvm, classpath=["C:\\Users\\Administrator\\JpypeTest.jar;"])

The error I get is:

Traceback (most recent call last):
  File ".\jpype_test.py", line 16, in <module>
    from blah.blah import Tester
ModuleNotFoundError: No module named 'blah'
PS C:\Users\Administrator> python .\jpype_test.py
C:\Users\Administrator\JpypeTest.jar;
Traceback (most recent call last):
  File ".\jpype_test.py", line 16, in <module>
    from blah.blah import Tester
ModuleNotFoundError: No module named 'blah'

I have built the JAR file a few different ways from Eclipse 2020/09 using JAR export and from the command line using:

C:\Users\Administrator\eclipse-workspace\JpypeTest\src>"C:\Java\OracleJDK-8_241_x64\bin"\jar cf C:\Users\Administrator\JpypeTest.jar blah\blah\*.class

I have confirmed that the JVM and the python interpreter are both 64 bit. I have also done my best to ensure the JVM is exactly the same between Eclipse, the command line and Python.

From what I can see, the JAR looks fine regardless of how I build it. It contains the blah\blah directory and the Tester.class file under it. The manifest is the only other file in the JAR.

I have also tried creating the JAR file with the class file at different directory levels (ie. one level of blah directories and no levels of blah directory).

Here are the versions of the python software:

Python 3.8.5 (default, Sep  3 2020, 21:29:08) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Package      Version
------------ -------------------
certifi      2020.6.20
JPype1       1.0.2
pip          20.2.3
setuptools   50.3.0.post20201006
wheel        0.35.1
wincertstore 0.2

I clearly have the base plumbing working since it can see the class file. Any thoughts on why the JAR is failing?

Thanks a bunch for your consideration.

1

There are 1 answers

0
Warwick Harvey On

Check that you don't have a directory called "blah" that python would look in for modules. This would hide your Java classes, as per https://jpype.readthedocs.io/en/latest/userguide.html#importing-java-classes :

One important caveat when dealing with importing Java modules. Python always imports local directories as modules before calling the Java importer. So any directory named java, com, or org will hide corresponding Java package. We recommend against naming directories as java or top level domain.