I have a jar file (not part of the Java standard library) that I want to use.
I’ve put myJavaPackage.jar into my project directory, and I’m trying to do import myJavaPackage, but I keep getting ImportError: No module named myJavaPackage. What am I doing wrong?
You need to make sure that Jython knows to search your .jar file for modules, which is done using pythonpath. This will differ depending on your setup, but in PyDev:
You should also check that the module name you are trying to import matches the package name as specified within the jar file. Note that the jar file might be named differently to the package, especially if there are multiple packages within it.
If you have the source for the .jar, open up the .java file containing the code you wish to utilise, and look for a line near the top that specifies the package. If you find a line that says something like
package foo.bar.myJavaPackage;, then you must do one ofimport foo.bar.myJavaPackage, and use the contents likeobj = foo.bar.myJavaPackage.someClass, orfrom foo.bar import myJavaPackage, and use the contents likeobj = myJavaPackage.someClass, orfrom foo.bar.myJavaPackage import someClass, and use it likeobj = myClass, but careful of name collisions using this method.