How can I add jars dynamically to jython, inside script?

Just add your jar to sys. Path, like this: ~ $ jython Jython 2.5.0+ (trunk:6691, Aug 17 2009, 17:09:38) Java HotSpot(TM) Client VM (Apple Computer, Inc. ) on java1.6.0-dp Type "help", "copyright", "credits" or "license" for more information.

>>> from org.thobe. Somepackage import SomeClass # not possible to import yet Traceback (most recent call last): File "", line 1, in ImportError: No module named thobe >>> import sys >>> sys.path. Append("/var/javalib/some-thobe-package.

Jar") # add the jar to your path >>> from org.thobe. Somepackage import SomeClass # it's now possible to import the package >>> some_object = SomeClass() # You can now use your java class It couldn't get more simple than that :) In your case you probably want to use the path of your package to find the jar: yourpackage/__init__. Py import sys, os if 'java' in sys.platform.lower(): sys.path.

Append(os.path. Join(os.path. Dirname(os.path.

Abspath(__file__)), "your-lib. Jar")) from jython_implementation import library else: from cpython_implementation import library Hope that helps!

Thx for reminding me how awesome jython was. – AndreasT Sep 23 at 8:30.

Up vote 4 down vote favorite 3 share g+ share fb share tw.

I am writing a package in python that talks to an ldap server. I want it to work in CPython and Jython. To get it to work with CPython, I have successfully coded against python-ldap.

However, to get it working with Jython, I must use a java jar. How can I distribute the jar file with my package, so that if it can "import java", it knows its jython, and dynamically adds the java jar to the path, and utilizies it. However, if that fails, it knows its CPython and uses the python-ldap libraries.

Any ideas? Jython link|improve this question asked Nov 13 '09 at 17:54gregturn481211 64% accept rate.

Just add your jar to sys. Path, like this: ~ $ jython Jython 2.5.0+ (trunk:6691, Aug 17 2009, 17:09:38) Java HotSpot(TM) Client VM (Apple Computer, Inc. ) on java1.6.0-dp Type "help", "copyright", "credits" or "license" for more information.

>>> from org.thobe. Somepackage import SomeClass # not possible to import yet Traceback (most recent call last): File "", line 1, in ImportError: No module named thobe >>> import sys >>> sys.path. Append("/var/javalib/some-thobe-package.

Jar") # add the jar to your path >>> from org.thobe. Somepackage import SomeClass # it's now possible to import the package >>> some_object = SomeClass() # You can now use your java class It couldn't get more simple than that :) In your case you probably want to use the path of your package to find the jar: # yourpackage/__init__. Py import sys, os if 'java' in sys.platform.lower(): sys.path.

Append(os.path. Join(os.path. Dirname(os.path.

Abspath(__file__)), "your-lib. Jar")) from jython_implementation import library else: from cpython_implementation import library Hope that helps!

Thx for reminding me how awesome jython was. – AndreasT Sep 23 '11 at 8:30 Unclear how "from org.thobe. Somepackage import SomeClass" is based off of the name of the package "some-thobe-package".

I gather that you need "from org. " but how does "some-thobe-package. Jar" translate to "thobe.

Somepackage" on the import? – delinquentme Feb 12 at 9:50 @delinquentme Since the jar file is in the path, its internal structure is what is referenced. If you were to run`jar -xvf /var/javalib/some-thobe-package.

Jar` You would see that the structure would be org/thobe/somepackage/SomeClass. Class – Eric Mar 13 at 19:09.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions