Thanks for the quick response. You saved my day. That did the trick!
-Alex *facepalm*
On 28.02.2011 17:54, Alex Grönholm wrote:
> 28.02.2011 18:47, Alexander Rosemann kirjoitti:
>> I try to create an instance of a Python class, that implements a Java
>> interface, in Java. Creating an instance of the Python class results in
>> a NPE.
>>
>> PyObject jyClass = interp.get("JythonClass");
>> PyObject jyObj = jyClass.__call__();
>>
>> The logs tell me that Jython was able to load the module though.
> You're not importing JythonClass to the local namespace at any point.
> After your import, all you have in the local namespace is "jythoncode".
> How about you do "from jythoncode.JythonModule import JythonClass"? Oh
> BTW, module names are supposed to be in lowercase too (not that it would
> cause errors, but still).
>> import: import jythoncode.JythonModule # loaded from
>> __pyclasspath__/jythoncode/JythonModule.py
>>
>> Any help is much appreciated. The eclipse project I am using is
>> available at http://www.scintillation.at/files/MyJythonTest.zip.
>>
>> Thanks,
>> Alex
>>
>> On 02.02.2011 13:19, Alexander Rosemann wrote:
>>> I am stuck again... for some reason I can't load the python class. When
>>> I call jyClass.__call__() I get a NPE even though the logs tell me that
>>> JythonModule has been loaded.
>>>
>>> If I am not misinterpreting the logs, Jython seems to load the all
>>> required class and modules:
>>> import: # trying orgjython/JythonModule.py
>>> __init__: self=P
>>> myMethod: self=P
>>> class JythonClass: __init__= myMethod=
>>> <file-top>: MyType= JythonClass=
>>> class JythonClass: __init__= myMethod=
>>> __init__: self=P
>>> myMethod: self=P
>>> import: trying orgjython/orgjava/__init__$py.class in sys class loader
>>> import: trying orgjython/orgjava/__init__.py in sys class loader
>>> import: trying orgjython/orgjava$py.class in sys class loader
>>> import: trying orgjython/orgjava.py in sys class loader
>>> import: trying source
>>> E:\Work\workspaces\terminalfour\MyJythonTest\__pyclasspath__\orgjava
>>> import: trying precompiled with no source
>>> E:\Work\workspaces\terminalfour\MyJythonTest\__pyclasspath__\orgjava$py.class
>>>
>>> import: trying orgjython as java class in sys.classLoader
>>> import: trying orgjava.MyType as java class in sys.classLoader
>>> import: 'MyType' as java class
>>> import: import orgjython.JythonModule # loaded from
>>> __pyclasspath__/orgjython/JythonModule.py
>>> Exception in thread "main" java.lang.NullPointerException
>>> at orgjava.JythonInterpreterTest.doIt(JythonInterpreterTest.java:107)
>>> at orgjava.JythonInterpreterTest.main(JythonInterpreterTest.java:141)
>>>
>>> Any clue what I am missing now?
>>>
>>> -Alex
>>>
>>>
>>> On 29.01.2011 22:44, Alexander Rosemann wrote:
>>>> I got it working using PlyJy by putting the JythonClass.py into a Lib
>>>> folder next to the jython.jar file. Now I have to figure out how to load
>>>> it from a package...
>>>>
>>>> On 29.01.2011 22:24, Josh Juneau wrote:
>>>>> I do not use Eclipse unfortunately...only Netbeans. I would recommend
>>>>> trying to place your .py files at the source root and try it...I have
>>>>> had luck doing that in the past.
>>>>>
>>>>> Josh Juneau
>>>>> juneau001@...
>>>>> http://jj-blogger.blogspot.com
>>>>> http://www.jythonpodcast.com
>>>>> Twitter ID: javajuneau
>>>>>
>>>>> On Jan 29, 2011, at 2:41 PM, Alexander
>>>>> Rosemann<alexander.rosemann@...> wrote:
>>>>>
>>>>>> Hi Josh! I tried it with PlyJy as well, again, it can't find the
>>>>>> python module. Do you have a small sample eclipse project which I
>>>>>> could use for testing? I'm running out of ideas troubleshooting the
>>>>>> issue...
>>>>>> Btw. I am using Java 1.6 x64.
>>>>>> -Alex
>>>>>>
>>>>>> On 29.01.2011 19:00, Josh Juneau wrote:
>>>>>>> Have you looked at the PlyJy project at all? It makes object factory
>>>>>>> creation much easier as it abstracts away much of the work you neec
>>>>>>> to do with the PythonInterpreter, etc.
>>>>>>>
>>>>>>> http://kenai.com/projects/plyjy
>>>>>>>
>>>>>>> For example, you can do something like the following:
>>>>>>> http://kenai.com/projects/plyjy/pages/JythonObjectFactory
>>>>>>>
>>>>>>> As far as the code goes, it looks ok...you really just need to
>>>>>>> include some method calls in your interface in order to interact
>>>>>>> with you jython object. I looks like you are trying to invoke
>>>>>>> "method", which should work as you have it. Are you sure that the
>>>>>>> Jython class is in your classpath? You may want to look at the
>>>>>>> classpath to be sure that it is being loaded.
>>>>>>>
>>>>>>> Josh Juneau
>>>>>>> juneau001@...
>>>>>>> http://jj-blogger.blogspot.com
>>>>>>> http://www.jythonpodcast.com
>>>>>>> Twitter ID: javajuneau
>>>>>>>
>>>>>>> On Jan 29, 2011, at 5:16 AM, Alexander
>>>>>>> Rosemann<alexander.rosemann@...> wrote:
>>>>>>>
>>>>>>>> Hi,
>>>>>>>>
>>>>>>>> I try calling a Python script from within Java using the object
>>>>>>>> factory
>>>>>>>> pattern as described in the Jython book. For some reason I can't load
>>>>>>>> the python class in Java.
>>>>>>>>
>>>>>>>> When executing the JythonInterpreterText class I get the following
>>>>>>>> error:
>>>>>>>> null null:null - null
>>>>>>>> Traceback (most recent call last):
>>>>>>>> File "<string>", line 1, in<module>
>>>>>>>> ImportError: cannot import name JythonModule
>>>>>>>>
>>>>>>>> I am using Jython 2.5.2. I am running the application from within
>>>>>>>> Eclipse. I have set up an ordinary Java project and added the
>>>>>>>> jython.jar
>>>>>>>> to the classpath. Any hints what I could try to resolve the issue are
>>>>>>>> much appreciated.
>>>>>>>>
>>>>>>>> That's my script:
>>>>>>>> from org.mypackage.test import MyType
>>>>>>>> import sys
>>>>>>>>
>>>>>>>> class JythonClass(MyType):
>>>>>>>> def __init__(self):
>>>>>>>> print 'init'
>>>>>>>>
>>>>>>>> def method(self):
>>>>>>>> print 'calling<method> from JythonClass'
>>>>>>>>
>>>>>>>> And that's the Java class:
>>>>>>>> package org.mypackage.test;
>>>>>>>>
>>>>>>>> import org.python.core.PyException;
>>>>>>>> import org.python.core.PyObject;
>>>>>>>> import org.python.util.PythonInterpreter;
>>>>>>>>
>>>>>>>> public class JythonInterpreterTest
>>>>>>>> {
>>>>>>>> public static void main(String[] args)
>>>>>>>> {
>>>>>>>> try
>>>>>>>> {
>>>>>>>> PythonInterpreter inter = new PythonInterpreter();
>>>>>>>> inter.exec("from org.mypackage.test import JythonModule");
>>>>>>>> PyObject jyClass = inter.get("JythonClass");
>>>>>>>> PyObject jyObj = jyClass.__call__();
>>>>>>>> MyType jaObj = (MyType) jyObj.__tojava__(MyType.class);
>>>>>>>> jaObj.method();
>>>>>>>> }
>>>>>>>> catch (PyException e)
>>>>>>>> {
>>>>>>>> e.normalize();
>>>>>>>> StringBuilder sb = new StringBuilder();
>>>>>>>> sb.append(e.value.__findattr__("filename"));
>>>>>>>> sb.append(" ");
>>>>>>>> sb.append(e.value.__findattr__("lineno"));
>>>>>>>> sb.append(":");
>>>>>>>> sb.append(e.value.__findattr__("offset"));
>>>>>>>> sb.append(" - ");
>>>>>>>> sb.append(e.value.__findattr__("msg"));
>>>>>>>> System.err.println(sb.toString());
>>>>>>>> e.printStackTrace(System.err);
>>>>>>>> }
>>>>>>>> } // main
>>>>>>>> }
>>>>>>>>
>>>>>>>> The interface looks like that:
>>>>>>>>
>>>>>>>> package org.mypackage.test;
>>>>>>>> public interface MyType { public void method(); }
>>>>>>>>
>>>>>>>> Best,
>>>>>>>> Alex
>>>>>>>>
>>>>>>>> ------------------------------------------------------------------------------
>>>>>>>>
>>>>>>>>
>>>>>>>> Special Offer-- Download ArcSight Logger for FREE (a $49 USD value)!
>>>>>>>> Finally, a world-class log management solution at an even better
>>>>>>>> price-free!
>>>>>>>> Download using promo code Free_Logger_4_Dev2Dev. Offer expires
>>>>>>>> February 28th, so secure your free ArcSight Logger TODAY!
>>>>>>>> http://p.sf.net/sfu/arcsight-sfd2d
>>>>>>>> _______________________________________________
>>>>>>>> Jython-users mailing list
>>>>>>>> Jython-users@...
>>>>>>>> https://lists.sourceforge.net/lists/listinfo/jython-users
>> ------------------------------------------------------------------------------
>> Free Software Download: Index, Search& Analyze Logs and other IT data in
>> Real-Time with Splunk. Collect, index and harness all the fast moving IT data
>> generated by your applications, servers and devices whether physical, virtual
>> or in the cloud. Deliver compliance at lower cost and gain new business
>> insights. http://p.sf.net/sfu/splunk-dev2dev
>> _______________________________________________
>> Jython-users mailing list
>> Jython-users@...
>> https://lists.sourceforge.net/lists/listinfo/jython-users
>
>
> ------------------------------------------------------------------------------
> Free Software Download: Index, Search& Analyze Logs and other IT data in
> Real-Time with Splunk. Collect, index and harness all the fast moving IT data
> generated by your applications, servers and devices whether physical, virtual
> or in the cloud. Deliver compliance at lower cost and gain new business
> insights. http://p.sf.net/sfu/splunk-dev2dev
> _______________________________________________
> Jython-users mailing list
> Jython-users@...
> https://lists.sourceforge.net/lists/listinfo/jython-users
|