Reputation: 12294
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?
Upvotes: 1
Views: 3532
Reputation: 12294
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 of
import foo.bar.myJavaPackage
, and use the contents like obj = foo.bar.myJavaPackage.someClass
, orfrom foo.bar import myJavaPackage
, and use the contents like obj = myJavaPackage.someClass
, orfrom foo.bar.myJavaPackage import someClass
, and use it like obj = myClass
, but careful of name collisions using this method.Upvotes: 3