Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import jar API in Jython

Tags:

import

jython

jar

I am trying to import a Java API, which is distributed as a jar file. I followed the instructions from this answer at a similar question in Stack Overflow, but it didn't work.

In Jython, I did:

>>> import sys
>>> sys.path.append("/path/to/jar/api")
>>> from com.thingmagic import *
Traceback (most recent calls last):
  File "<stdin>", line 1, in <module>
ImportError: no module named thingmagic

Am I missing something or did I do something wrong?

like image 708
iomartin Avatar asked Jul 27 '12 18:07

iomartin


People also ask

How do I import a module into jython?

You can do the easy_install for jython. Then next time you startup jython you will be able to import it. The official docs are here: jython.org/jythonbook/en/1.0/…

How do I add a jar file to Python?

register 'script.py' using jython as script_udf; a = LOAD 'data. json' USING PigStorage('*') as (line:chararray); teams = FOREACH a GENERATE script_udf.

What is Jar API?

An API is an Application Programming Interface, so it defines the methods you can use. ( wikipedia link) A JAR is a Java Archive, it is just a packaged Java Application. ( wikipedia link)


2 Answers

You need to provide the full path of the JAR file. Change

sys.path.append("/path/to/jar/api")

to

sys.path.append("/path/to/jar/api/whatever_the_name_is.jar")
like image 139
mzjn Avatar answered Nov 11 '22 11:11

mzjn


The problem was that I was using only one backslash on the path (I'm developing on Windows), instead of two:

sys.path.append("C:\\remember\\to\\use\\two\\backaslashes\\jarName.jar")
like image 24
iomartin Avatar answered Nov 11 '22 10:11

iomartin