Page 1 of 1

Importing JAVA Libraries in Python

Posted: 29 Mar 2009, 12:47
by yaswanth
Hi,

I am stuck in importing JAVA libraries to Python.

1. How do I import JSON?
I tried using simplejson with SFS Python, but its throwing some errors. So I didnt want to use external libs and wanted to use core JAVA libs. Is there a way I can directly import Java's JSON Classes? I have copied json-lib into the lib directory and when I am trying to import the classes using

Code: Select all

import org.json
or 
import java.net.json
or
something else
Its not importing it, it says no module name json.

I am using eclipse to test and tried putting all the jars in the library path and class path too but to no avail.

2. For connecting to my Mysql DB. Do I have to use hibernate or can I use normal jdbc dirver to connect to my DB server. If so, if someone can give me a code snippet of how to import it, it would be great.

Thirdly, if I create a singleton class to run in an extension, will the same object be used if I use the class in another extension? Or since its a threaded execution will there be any difference?
Thank You.

Posted: 31 Mar 2009, 05:29
by Lapo
It should be:
from org.json import JSONObject, JSONArray

Then you can use them directly in your Python code.

Here are the docs:
http://www.json.org/javadoc/org/json/JSONObject.html
http://www.json.org/javadoc/org/json/JSONArray.html

p.s. = native Python libraries won't work in Jython unless they are written in pure Python. In any case you'll need the sources to use them. The C-Python bytecode is not compatible with Jython

SimpleJSON in Jython

Posted: 01 Apr 2009, 21:13
by yaswanth
As I mentioned above, I tried using simplejson and it throws an error which says 'invalid syntax' near 'yield'.

After some digging, I came to know that 'yield' doesnt work in Jython 2.1.

So the best way to do this is us

Code: Select all

from __future__ import generators
At the beginning of the file (not anywhere else).

And everything works like a charm.

And coming down to org.json import, eclipse is unable to show some classes in the interface otherwise org.json works perfectly fine!

Thanks Lapo. Without you, 90% of the forum will be inactive :-)