1

Using Eclipse + PyDev + jython. Need to import a java package to use a Java class inside a Python program (using Max OSX).

For import, I mean statement in Python like from com.a.b.c. Wondering where should I put the Java jar file which contains com.a.b.c? Thanks.

BTW, if any PyCharm + jython based solution, it will be also great. :)

This question is not duplicate from the other one, the other one's title is bit mis-leading, and that one is about how to install jython.

Community
  • 1
  • 1
Lin Ma
  • 9,739
  • 32
  • 105
  • 175
  • 1
    Then please edit the previous question (and this one) to clarify exactly what they both mean, and please remove salutations etc. – jonrsharpe Jul 05 '16 at 07:58
  • @jonrsharpe, thanks and vote up for your comments. I have removed salutation, and edit titles to make it more clear. – Lin Ma Jul 05 '16 at 17:01
  • 1
    Did you read [the documentation](http://www.jython.org/jythonbook/en/1.0/ModulesPackages.html)? Are you having any specific problem? – Lie Ryan Jul 08 '16 at 02:05
  • @LieRyan, nice document and vote up, want to confirm I can put the jar file under any directory, as long as I am using `sys.path.append`, for example, `sys.path.append("/Users/frank/lib/mysql-connector-java-5.1.6.jar")` to indicate where the jar is? Thanks. – Lin Ma Jul 08 '16 at 05:04

1 Answers1

3

The import semantics do not differ that much from CPythons from what I see in the Jython Docs.

First a search is made for the .jar file in the current directory; if it is not found there, it is looked up in the directory containing the core Jython libraries. The classpath which corresponds to Javas CLASSPATH is then searched along with the site-packages directory containing external libaries. I am not yet sure what __pyclasspath__ is.

So if a package is not found in those directories an import error is raised. You have two options:

  • Either add the .jar in one of the directories (typically you should never add it to the directory containing the core libs.
  • Add the .jar to your CLASSPATH.
  • Add the path to your .jar in sys.path.

For the first case, either move it to the current direcory or in site-packages.

For the second case see here on how to add a .jar to your CLASSPATH.

For the third simply call sys.path.append("path_to_jar") to include the directory containing your .jar file to sys.path.

Community
  • 1
  • 1
Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253
  • Thanks Jim, vote up. I am using IDE like PyCharm, how do I know what is current directory for a project? Any Python command I can use? – Lin Ma Jul 08 '16 at 17:40
  • BTW, another question is, in order to be able to import java packages, I have to use jython other than CPython? Thanks. – Lin Ma Jul 08 '16 at 17:40
  • BTW, also, if there a solution, which I can just put all the jars under a directory (then I can use import directly), without explicitly writing full path and jar names, (e.g. `sys.path.append("/Users/frank/lib/mysql-connector-java-5.1.6.jar")`), it will be great. :) – Lin Ma Jul 08 '16 at 17:58
  • 1
    The current directory is the directory containing your `.py` files (or use `import os; print(os.getcwd())` to get a path. As for your other questions: a) Yes in order to `import` from java packages, you need to use `Jython`, `CPython` has no way of doing this. b) You could put everything in the `site-packages` directory. If you `print(sys.path)` it should have a value that points to the directory of `site-packages`; you should be able to use that. – Dimitris Fasarakis Hilliard Jul 08 '16 at 18:52
  • Thanks Jim, vote up for your reply. Followed your guidance, I checked the site-package directory is `/Users/foo/jython2.7.1b3/Lib/site-packages`. The question is, suppose there are multiple jar files, like `goo.jar`, `zoo.jar`, etc. which my program may use to import. If I use statement `from com.a.b.c import fooClass`, how did Jython do? It scan all jars under site-packaged directory `goo.jar`, `zoo.jar`, etc.? Suppose `com.a.b.c.fooClass` is defined in `zoo.jar`, scan `goo.jar` to see it defines `com.a.b.c.fooClass` seems a waste of time? Thanks. – Lin Ma Jul 08 '16 at 22:25
  • 1
    @Lin Ma: java's import semantic merges package namespaces from different jars/packages, that's why it always have to scan every jars, whether or not something is already found. According to docs: `If you only use full class imports, you can skip the package scanning altogether. Set the system property python.cachedir.skip to true or (again) pass in your own postProperties to turn it off.` – Lie Ryan Jul 09 '16 at 02:21
  • Thanks @LieRyan, vote up for your reply. What do you mean full class imports? `from com.a.b.c import fooClass` is a full class import example? – Lin Ma Jul 09 '16 at 06:04
  • Hi Jim, thanks for all the help, mark your reply as answer and grant the bounty points. :) – Lin Ma Jul 12 '16 at 23:54