From: James H. <jh...@pa...> - 2001-06-15 22:29:06
|
Hi, So far I think Jython is pretty cool... but I've got a problem/question... First off, here's my sample code: "MyMap.java" =========================================== import java.io.Serializable; import java.util.HashMap; public class MyMap { private HashMap hash = new HashMap(10); public void MyMap() { } public void setAttribute(String key, Object attr) { hash.put(key, attr); } public Object getAtrribute(String key) { return hash.get(key); } public static void main(String[] args) { Properties props = new Properties(); props.setProperty("jython.home", "/jython"); PythonInterpreter.initialize(System.getProperties(),props,new String[0]); MyMap theMap = new MyMap(); theMap.setAttribute("myAttr", "myAttr is cool"); PythonInterpreter interp = new PythonInterpreter(); interp.set("theMap", theMap); interp.exec("print theMap.getAtrribute(\"myAttr\")"); interp.exec("theMap.setAttribute(\"crapKey\", \"crapVal\")"); MyMap newMap = (MyMap)interp.get("theMap", MyMap.class); System.err.println("GotBack: " + (String)newMap.getAtrribute("crapKey")); } } ======================================== This works just fine, as intended by printing the output: myAttr is cool GotBack: crapVal My problem is that I really need the method void setAttribute(String key, Object attr) to be this: void setAttribute(String key, Serializable attr) But when I make that change, rather than the expected output, I get: myAttr is cool Exception in thread "main" java.lang.ClassCastException: org.python.core.PyString at com.partnet.prototypes.mops.unittest.MyMap.main(MyMap.java:72) Can someone explain this to me? I really need to restrict the input objects to be of type Serializable. Thanks, James |
From: Raj B. <r0...@co...> - 2003-01-10 16:01:31
|
Hello, I am new to Jython/Java. I am having problems running a basic applet (as well as the ones that came as part of install) I have JDk1.3.1_06, Win2K, Jython 2.1 My classpath includes (among other things) c:\jdk1.3.1_06;c:\jdk1.3.1_06\lib;c:\jython-21\jython.jar I can compile my example.py (and the demos.py) fine, no errors. But as I try to run the applet I get the error java.lang.NoClassDefFoundError: org/python/core/PyObject in my Sun Java console Any suggestions would be greatly appreciated. raj |
From: Danny A. <dan...@bt...> - 2001-06-16 08:06:27
|
I'm guessing, but it might be worth trying to cast the Serializable into an object before putting it in the hashtable. --- Danny Ayers http://www.isacat.net >-----Original Message----- >From: jyt...@li... >[mailto:jyt...@li...]On Behalf Of James >House >Sent: 15 June 2001 23:29 >To: jyt...@li... >Subject: [Jython-users] Newbie question > > > >Hi, > >So far I think Jython is pretty cool... but I've got a problem/question... > >First off, here's my sample code: > >"MyMap.java" >=========================================== >import java.io.Serializable; >import java.util.HashMap; > >public >class MyMap >{ > private HashMap hash = new HashMap(10); > > public > void MyMap() > { > } > > public > void setAttribute(String key, Object attr) > { > hash.put(key, attr); > } > > public > Object getAtrribute(String key) > { > return hash.get(key); > } > > public static > void main(String[] args) > { > Properties props = new Properties(); > props.setProperty("jython.home", "/jython"); > PythonInterpreter.initialize(System.getProperties(),props,new >String[0]); > > MyMap theMap = new MyMap(); > > theMap.setAttribute("myAttr", "myAttr is cool"); > > PythonInterpreter interp = new PythonInterpreter(); > interp.set("theMap", theMap); > > interp.exec("print theMap.getAtrribute(\"myAttr\")"); > interp.exec("theMap.setAttribute(\"crapKey\", \"crapVal\")"); > > MyMap newMap = (MyMap)interp.get("theMap", MyMap.class); > System.err.println("GotBack: " + >(String)newMap.getAtrribute("crapKey")); > > } >} > >======================================== > >This works just fine, as intended by printing >the output: > > myAttr is cool > GotBack: crapVal > > >My problem is that I really need the method > > void setAttribute(String key, Object attr) > >to be this: > > void setAttribute(String key, Serializable attr) > >But when I make that change, rather than the >expected output, I get: > > myAttr is cool > Exception in thread "main" java.lang.ClassCastException: >org.python.core.PyString > at >com.partnet.prototypes.mops.unittest.MyMap.main(MyMap.java:72) > > >Can someone explain this to me? I really need >to restrict the input objects to be of type Serializable. > >Thanks, >James > > >_______________________________________________ >Jython-users mailing list >Jyt...@li... >http://lists.sourceforge.net/lists/listinfo/jython-users |
From: <bc...@wo...> - 2001-06-18 18:21:05
|
On Fri, 15 Jun 2001 16:28:33 -0600, you wrote: >Hi, > >So far I think Jython is pretty cool... but I've got a problem/question... > >First off, here's my sample code: > >... > >My problem is that I really need the method > > void setAttribute(String key, Object attr) > >to be this: > > void setAttribute(String key, Serializable attr) Jython-2.0 did not handle Serializable argument very well. This have been fixed in jython-2.1a1 where your example works. >Can someone explain this to me? When the python code theMap.setAttribute("crapKey", "crapVal") is executed jython is faced with a choice. Should it insert the the PyString instance that wraps "crapVal" or should it unwrap the string and insert the java.lang.String instance. If the method signature uses Object, jython always unwraps, which is why that works in you example. If the method signature used Serializable, Jython-2.0 would check and see that PyString implement Serializable (it does) and pass over the PyString wrapper. In Jython-2.1a1, a Serializable signature will cause unwrapping of string, ints, longs, floats, classes and instances. regards, finn |
From: James H. <jh...@pa...> - 2001-06-18 20:27:09
|
Thank you for the clear explanation... I'll look forward to the new version! James At 6/18/2001 06:22 PM +0000, Finn Bock wrote: >On Fri, 15 Jun 2001 16:28:33 -0600, you wrote: > > >Hi, > > > >So far I think Jython is pretty cool... but I've got a problem/question... > > > >First off, here's my sample code: > > > >... > > > >My problem is that I really need the method > > > > void setAttribute(String key, Object attr) > > > >to be this: > > > > void setAttribute(String key, Serializable attr) > >Jython-2.0 did not handle Serializable argument very well. This have >been fixed in jython-2.1a1 where your example works. > > >Can someone explain this to me? > >When the python code > > theMap.setAttribute("crapKey", "crapVal") > >is executed jython is faced with a choice. Should it insert the the >PyString instance that wraps "crapVal" or should it unwrap the string >and insert the java.lang.String instance. If the method signature uses >Object, jython always unwraps, which is why that works in you example. >If the method signature used Serializable, Jython-2.0 would check and >see that PyString implement Serializable (it does) and pass over the >PyString wrapper. In Jython-2.1a1, a Serializable signature will cause >unwrapping of string, ints, longs, floats, classes and instances. > >regards, >finn |