One more thing: it's a known problem that Clamp does not currently build jars correctly on Windows. Another issue seen on Windows is sys.executable does not get set. See the Clamp issues in https://github.com/jythontools/clamp/

To address both the Clamp issues and in Jython proper, as of yesterday I now have a MSDN license so that we can test in this environment. (This license is courtesy of a program Microsoft has for Python committers.) Hopefully this will help get us to better Windows support for Jython.

- Jim




On Wed, Apr 9, 2014 at 11:31 AM, Jim Baker <jbaker@zyasoft.com> wrote:
This is the exact use case for Clamp. If interested, I have a write up that introduces this project; https://github.com/jimbaker/clamped, along with a talk, https://github.com/jimbaker/clamped/blob/master/talk.pdf

Among other things, Clamp ensures that your Python class, as wrapped by a Java proxy, can be resolved by Class.forName or be directly imported from Java. Clamp is also integrated with setuptools and is site-packages aware.

Here's an example of Clamp in action. Let's assume the BarClamp class is defined in the Python module clamped; then BarBase is setting it up so that the classname for Java will be ".".join(["bar", "clamped", "BarClamp"]) or bar.clamped.BarClamp. That prefix is arbitrary; it could be com.example.baz:

from java.io import Serializable
from java.util.concurrent import Callable
from clamp import clamp_base

BarBase = clamp_base("bar")  # metaclass to map to Java packages

class BarClamp(BarBase, Callable, Serializable):
    def call(self):
        return 42
To then use that clamped class, you can simply import it:


import bar.clamped.BarClamp;  // yes, you can now just import Python classes!

public class UseClamped {
    public static void main(String[] args) {
        BarClamp barclamp = new BarClamp();
        try {
            System.out.println("BarClamp: " + barclamp.call());
        } catch (Exception ex) {
            System.err.println("Exception: " + ex);
        }
    }
}

Clamp is under active development, but it's definitely stable enough for testing.

- Jim 


On Wed, Apr 9, 2014 at 11:03 AM, Alan Kennedy <jython-dev@xhaus.com> wrote:
Hi Richard,

Without seeing your java code, it's hard to state what exactly might be wrong.

But there is a simple conversion process that you need to go through to obtain a java object equivalent of a class implemented in jython.

The best way to illustrate this is with code that does it. Modjy is one example in the code base, that uses a servlet implemented in jython to provide WSGI capability to servlet containers.

The key piece of code is here.

http://hg.python.org/jython/file/45b3007473f9/src/com/xhaus/modjy/ModjyJServlet.java#l116

You should try that approach.

Alan.



On Tue, Apr 8, 2014 at 9:58 AM, Richard Eckart de Castilho <richard.eckart@gmail.com> wrote:
Hi all,

I want to define a class in Jython and instantiate it in Java.

I am using a Java framework which allows users to create custom
components (classes implementing specific interfaces). The framework
is built in such a way that it instantiates these components itself
based on a description containing a class name and a set of parameters.

My goal is to define a new component within Jython and to use it with
the framework. Here some pseudocode:

class MyComponent(Component):
 def run(self, *args):
   // do something

run(createComponent(MyComponent))

(btw. in Java I would have written: run(createComponent(MyComponent.class))

That gives me an error along the lines of

InitializationException: Component class "org.python.proxies.__main__$MyComponent$1" was not found.

Reading a bit in [1], I now believe that what I want to do is not really
possible without modifying the framework that I am using because classes
defined in Jython do not actually have a java.lang.Class instance
associated with them and there is actually no classloader which would be able
to return such an instance when Class.forName("org.python.proxies.__main__$MyComponent$1")
is called.

Is this correct, or is there a way to do what I want to do?

Cheers,

-- Richard

[1] http://www.jython.org/jythonbook/en/1.0/JythonAndJavaIntegration.html
------------------------------------------------------------------------------
Put Bad Developers to Shame
Dominate Development with Jenkins Continuous Integration
Continuously Automate Build, Test & Deployment
Start a new project now. Try Jenkins in the cloud.
http://p.sf.net/sfu/13600_Cloudbees
_______________________________________________
Jython-users mailing list
Jython-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jython-users


------------------------------------------------------------------------------
Put Bad Developers to Shame
Dominate Development with Jenkins Continuous Integration
Continuously Automate Build, Test & Deployment
Start a new project now. Try Jenkins in the cloud.
http://p.sf.net/sfu/13600_Cloudbees
_______________________________________________
Jython-users mailing list
Jython-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jython-users