Using JAR File distributions in Jython

Ype Kingma ykingma at accessforall.nl
Fri Jan 10 22:25:53 CET 2003


VLP wrote:

> I have an API distributed in JAR form, that is currently being used by
> some Java programs. I would like to use this API from Jython. I read
> in an earlier post, that I must set CLASSPATH to include the JAR file
> itself, not just the directory it is in, so I did accordingly. This
> seems to have some effect, as the next time Jython started up, it
> recognized the JAR file, and did some processing (notably adding it to
> the cachedir package cache, and changing package.idx)

When jython does not repeat such indexing the next time you call jython
with the jar on the class path you have the expected behaviour.
 
> However, I still cannot "import" any classes from the JAR file. I
> tried following forms:
> 
> import com.company.product.api.class
> and
> from com.company.product.api import class
> and
> from com.company.product.api import *
> 
> I get the error:  No module named company

You might try this interactively:

Jython 2.1 on java1.3.1_04 (JIT: null)
Type "copyright", "credits" or "license" for more information.
>>> import com
>>> dir(com)
['__name__', 'sun', 'ziclix']

to see what things are available in com. In this case there
is nothing but the minimum on the class path (jython.jar).
When there is a package com.company in a jar on the class path
you should also see company mentioned.
Note that 'company' should be a public java package.
When it's not public you can tell jython to access it as if
it were public (using a jython registry option) 
but that's not recommended.

> The JAR file is called productapi.jar and was created using JDK
> 1.3.1_06. I am using the same version of JDK to run Jython. I am using
> Jython 2.1
> 
> Can someone point out what I am doing wrong here, and how I can get
> this to work? This is extremely important, as without this, I will be
> unable to replace Java with Jython in this project...which is a
> tragedy, because Java is so much more painful to work with!

You seem to be doing it right.

Typing this to jython might help:
import java
java.lang.System.getProperties()['java.class.path']

Good luck,
Ype





More information about the Python-list mailing list