Quantcast

Problems using Jython with One-Jar

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Problems using Jython with One-Jar

Jacek
Hi,

We have a large Java Spring web app that we've converted to the One-Jar model (with embedded Jetty as the web server).
It integrates Jython to provide support for customer-specific code in particular parts of the application
which we initialize like this:

@PostConstruct
public void init() {
Properties props = new Properties();
props.setProperty("python.path", "/opt/software/custom");
PythonInterpreter.initialize(System.getProperties(), props, new String[] { "" });
interpreter = new PythonInterpreter();
}

with Jython scripts deployed into the /opt/software/custom folder where we pick them up and execute them from the parent Java logic to allow for customer-specific customizations.

Once we converted to the One-Jar model, everything else works...except particular Jython scripts.
Namely those that have an import statement, e.g.:

from datetime import datetime

When Jython encounters a script with any import in it it throws:

org.python.core.PyException: null
at org.python.core.Py.ImportError(Py.java:290) ~[jython-full-2.5.2.jar:na]
at org.python.core.imp.import_first(imp.java:750) ~[jython-full-2.5.2.jar:na]
at org.python.core.imp.import_name(imp.java:834) ~[jython-full-2.5.2.jar:na]
at org.python.core.imp.importName(imp.java:884) ~[jython-full-2.5.2.jar:na]
at org.python.core.ImportFunction.__call__(__builtin__.java:1220) ~[jython-full-2.5.2.jar:na]
at org.python.core.PyObject.__call__(PyObject.java:357) ~[jython-full-2.5.2.jar:na]
at org.python.core.__builtin__.__import__(__builtin__.java:1173) ~[jython-full-2.5.2.jar:na]
at org.python.core.imp.importFromAs(imp.java:978) ~[jython-full-2.5.2.jar:na]
at org.python.core.imp.importFrom(imp.java:954) ~[jython-full-2.5.2.jar:na]

if a script does not have an import statement (some of the very simple ones), they work just fine.

I've seen someone had the same issue, as mentioned in the EnigmaCurry blog:


See comment at the very bottom

Nice article! However, when i tried to use it for my app, I found that it was unable to find the Python libraries in the jar. On my development machine, they were found ok, but when I deployed to a machine which has neither Python nor Jython installed, it failed to load datetime. To make sure it wasn't some peculiarity of my app, I tweaked the provided sample app, adding 'import datetime' to the __init__.py. This gave the same failure. I suspect the issue is that Jython loads Python files using an explicit classloader, and therefore bypasses the one-jar classloader, but I haven't had time or inclination to verify this. 

If anyone could point us into what we need to change in the way we initialize the PythonInterpreter to make it support imports in a One-Jar environment, it would be greatly appreciated...the Definiteive Guide to Jython book mentions it as working out of the box, so I presume this is some recent regression.

Much appreciated

Jacek

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Jython-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jython-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Problems using Jython with One-Jar

Johannes Buchner
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Dear Jacek,

although I don't have a solution to your specific problem, I just want
you to know that if you use maven already with your project, there is
the possibility of using the
   Maven Jython Mojos http://mavenjython.sf.net/
which package the Jython runtime and all desired Python libraries
automatically next to your Java code for a deployable jar.
You can find all relevant links here:
http://wiki.python.org/jython/JythonFaq/DistributingJythonScripts#Using_Maven

In debugging it helps to print the current python class path used for
imports (sys.path).

Cheers,
Johannes

On 05/09/12 15:54, Jacek Furmankiewicz wrote:

> Hi,
>
> We have a large Java Spring web app that we've converted to the
> One-Jar model (with embedded Jetty as the web server). It
> integrates Jython to provide support for customer-specific code in
> particular parts of the application which we initialize like this:
>
> @PostConstruct public void init() { Properties props = new
> Properties(); props.setProperty("python.path",
> "/opt/software/custom");
> PythonInterpreter.initialize(System.getProperties(), props, new
> String[] { "" }); interpreter = new PythonInterpreter(); }
>
> with Jython scripts deployed into the /opt/software/custom folder
> where we pick them up and execute them from the parent Java logic
> to allow for customer-specific customizations.
>
> Once we converted to the One-Jar model, everything else
> works...except particular Jython scripts. Namely those that have an
> import statement, e.g.:
>
> from datetime import datetime
>
> When Jython encounters a script with any import in it it throws:
>
> org.python.core.PyException: null at
> org.python.core.Py.ImportError(Py.java:290)
> ~[jython-full-2.5.2.jar:na] at
> org.python.core.imp.import_first(imp.java:750)
> ~[jython-full-2.5.2.jar:na] at
> org.python.core.imp.import_name(imp.java:834)
> ~[jython-full-2.5.2.jar:na] at
> org.python.core.imp.importName(imp.java:884)
> ~[jython-full-2.5.2.jar:na] at
> org.python.core.ImportFunction.__call__(__builtin__.java:1220)
> ~[jython-full-2.5.2.jar:na] at
> org.python.core.PyObject.__call__(PyObject.java:357)
> ~[jython-full-2.5.2.jar:na] at
> org.python.core.__builtin__.__import__(__builtin__.java:1173)
> ~[jython-full-2.5.2.jar:na] at
> org.python.core.imp.importFromAs(imp.java:978)
> ~[jython-full-2.5.2.jar:na] at
> org.python.core.imp.importFrom(imp.java:954)
> ~[jython-full-2.5.2.jar:na]
>
> if a script does not have an import statement (some of the very
> simple ones), they work just fine.
>
> I've seen someone had the same issue, as mentioned in the
> EnigmaCurry blog:
>
> http://www.enigmacurry.com/2009/05/20/distributing-jython-apps-in-a-single-jar-file/
>
>  See comment at the very bottom
>
> /Nice article! However, when i tried to use it for my app, I found
> that it was unable to find the Python libraries in the jar. On my
> development machine, they were found ok, but when I deployed to a
> machine which has neither Python nor Jython installed, it failed to
> load datetime. To make sure it wasn't some peculiarity of my app, I
> tweaked the provided sample app, adding 'import datetime' to the
> __init__.py <http://__init__.py/>. This gave the same failure. I
> suspect the issue is that Jython loads Python files using an
> explicit classloader, and therefore bypasses the one-jar
> classloader, but I haven't had time or inclination to verify this.
> / / / If anyone could point us into what we need to change in the
> way we initialize the PythonInterpreter to make it support imports
> in a One-Jar environment, it would be greatly appreciated...the
> Definiteive Guide to Jython book mentions it as working out of the
> box, so I presume this is some recent regression.
>
> Much appreciated
>
> Jacek
>
>
> ------------------------------------------------------------------------------
>
>
Live Security Virtual Conference

> Exclusive live event will cover all the ways today's security and
> threat landscape has changed and how IT managers can respond.
> Discussions will include endpoint security, mobile security and the
> latest in malware threats.
> http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
>
>
>
> _______________________________________________ Jython-users
> mailing list [hidden email]
> https://lists.sourceforge.net/lists/listinfo/jython-users

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.17 (GNU/Linux)

iEYEARECAAYFAk+qesIACgkQ7X1+MfqVcr23hQCdFXDE7A4oDUG7PsL79JTuYVtG
cTQAn0iAEFjCEoKdf5mP0UR2b/ex5Meh
=ZG7r
-----END PGP SIGNATURE-----

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Jython-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jython-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Problems using Jython with One-Jar

Jacek
In reply to this post by Jacek
I have found a workaround...but it is very ugly.

I take all the contents of the Lib folder (e.g. datetime.py, etc) and copy them as resources into one of our JARs, so that Maven bundles them.

With that Jython is able to find them for imports...but for some reason is not able to find it when it is in the jython-standalone.jar itself,
while running in a One-Jar classloader.

If anyone can suggest a better (and more maintainable) solution it would be greatly appreciated

Jacek

On Wed, May 9, 2012 at 9:54 AM, Jacek Furmankiewicz <[hidden email]> wrote:
Hi,

We have a large Java Spring web app that we've converted to the One-Jar model (with embedded Jetty as the web server).
It integrates Jython to provide support for customer-specific code in particular parts of the application
which we initialize like this:

@PostConstruct
public void init() {
Properties props = new Properties();
props.setProperty("python.path", "/opt/software/custom");
PythonInterpreter.initialize(System.getProperties(), props, new String[] { "" });
interpreter = new PythonInterpreter();
}

with Jython scripts deployed into the /opt/software/custom folder where we pick them up and execute them from the parent Java logic to allow for customer-specific customizations.

Once we converted to the One-Jar model, everything else works...except particular Jython scripts.
Namely those that have an import statement, e.g.:

from datetime import datetime

When Jython encounters a script with any import in it it throws:

org.python.core.PyException: null
at org.python.core.Py.ImportError(Py.java:290) ~[jython-full-2.5.2.jar:na]
at org.python.core.imp.import_first(imp.java:750) ~[jython-full-2.5.2.jar:na]
at org.python.core.imp.import_name(imp.java:834) ~[jython-full-2.5.2.jar:na]
at org.python.core.imp.importName(imp.java:884) ~[jython-full-2.5.2.jar:na]
at org.python.core.ImportFunction.__call__(__builtin__.java:1220) ~[jython-full-2.5.2.jar:na]
at org.python.core.PyObject.__call__(PyObject.java:357) ~[jython-full-2.5.2.jar:na]
at org.python.core.__builtin__.__import__(__builtin__.java:1173) ~[jython-full-2.5.2.jar:na]
at org.python.core.imp.importFromAs(imp.java:978) ~[jython-full-2.5.2.jar:na]
at org.python.core.imp.importFrom(imp.java:954) ~[jython-full-2.5.2.jar:na]

if a script does not have an import statement (some of the very simple ones), they work just fine.

I've seen someone had the same issue, as mentioned in the EnigmaCurry blog:


See comment at the very bottom

Nice article! However, when i tried to use it for my app, I found that it was unable to find the Python libraries in the jar. On my development machine, they were found ok, but when I deployed to a machine which has neither Python nor Jython installed, it failed to load datetime. To make sure it wasn't some peculiarity of my app, I tweaked the provided sample app, adding 'import datetime' to the __init__.py. This gave the same failure. I suspect the issue is that Jython loads Python files using an explicit classloader, and therefore bypasses the one-jar classloader, but I haven't had time or inclination to verify this. 

If anyone could point us into what we need to change in the way we initialize the PythonInterpreter to make it support imports in a One-Jar environment, it would be greatly appreciated...the Definiteive Guide to Jython book mentions it as working out of the box, so I presume this is some recent regression.

Much appreciated

Jacek


------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Jython-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jython-users
Loading...