Importing Java Class from a Python (Jython) Script fails

96 Views Asked by At

I need to automate execution of a small programm written in Java. The programm has an API that can be accessed through a Python script that I am calling from Jython-Standalone. My Python script needs to import two java classes (modules) but only one is recognized. Furtheremore, I am trying to automate execution of the script through a batch file.

The Python script resides outside the *.jar file in which the java classes are located, thus I am inserting the *.jar path with sys.path.insert. So far the code looks like this:

import sys
sys.path.insert(0, 'C:/Program Files/HEC/HEC-HMS/4.10/lib/vortex-0.10.30.jar')
from mil.army.usace.hec.vortex.io import BatchImporter
print('Imported BatchImporter')
from mil.army.usace.hec.vortex.geo import WktFactory

This results in:

Imported BatchImporter

ImportError: No module named geo

What is weird is that the first import works properly but not the second one.

mil.army.usace.hec.vortex.io and mil.army.usace.hec.vortex.geo is the directory structure containing the Java classes and I have checked and double checked and the classes are in did under this structure.

I thought maybe there is some weird character on "mil.army.usace.hec.vortex.geo" so I have tried all sorts of capital and small letters combinations to no avail.

I am not a programmer and have never had any experience with Java or Jython, so plase bare with me if I am not expressing the whole issue properly.

In case it is useful, here is the BAT file content I am using to call the met_data_import.py script.

SET "HMS_HOME=C:\Program Files\HEC\HEC-HMS\4.10"
SET "PATH=%HMS_HOME%\bin;%HMS_HOME%\bin\gdal;%PATH%"
SET "PROJ_LIB=%HMS_HOME%\bin\gdal\projlib"
SET "GDAL_DATA=%HMS_HOME%\bin\gdal\gdal-data"
SET "CLASSPATH=%HMS_HOME%\lib\*"
"%HMS_HOME%\jre\bin\java.exe" -jar C:\Users\..\..\jython-standalone-2.7.3.jar C:\Users\..\..\..\GRID_Importer\met_data_import.py

Some things to consider:

  • I would like to keep using jython-standalone if possible.
  • I cannot modify anything within the *.jar file.

Thanks for any help. I will be happy to improve the question if needed.

Following @Charles Duffy advice I searched inside the jar file through the CMD (by the way I am in a Windows environment): tar -tf vortex-0.10.30.jar | findstr geo and for comparisson ...| findstr io and here is what it spitted out:

mil/army/usace/hec/vortex/geo/...

mil/army/usace/hec/vortex/geo/WktFactory.class...

and

mil/army/usace/hec/vortex/io/...

mil/army/usace/hec/vortex/io/BatchImporter.class...

0

There are 0 best solutions below