ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> Jython-users
Jython-users
AW: [Jython-users] call a java-method from embedded interpreter
by Kay other posts by this author
Oct 14 2003 12:49PM messages near this date
RE: [Jython-users] call a java-method from embedded interpreter | Re: AW: [Jython-users] call a java-method from embedded interpreter
Thank you for your answers. Now i know where the mistake is.
The way you show, only works if the java-method is declared
'static'.
But in my example the java-method 'run' is non-static and
causes an error if i start it with the interpreter.

Is there a way to start non-static java-method with the interpreter?

Regards,
Kay


-----Urspr|ngliche Nachricht-----
Von: Jeff Emanuel [mailto:JEmanuel@[...].com]
Gesendet: Montag, 13. Oktober 2003 17:30
An: 'Kay '
Cc: 'jython-users@lists.sourceforge.net'
Betreff: RE: [Jython-users] call a java-method from embedded interpreter


In addition to what Diez said, you could change

> PyObject pyObject = interp.eval("obj1");
> pyObject.__getattr__("run").__call__(); <--- doesn't work?!

to

MyClass myObject = (MyClass)interp.get("obj1",MyClass.class);
myObject.run();




Hi,
>  interp = new PythonInterpreter();
>  interp.set("obj1", obj);
>  PyObject pyObject = interp.eval("obj1");
>  pyObject.__getattr__("run").__call__(); <--- doesn't work?!

Why so complicated? Why don't you just do

PyObject pyObject = interp.eval("obj1.run()");

Yielding null here, you could go for "exec" of course.

Or just

obj.run()

- as you don't do anything "pythonic" there :)

I'm not sure why you get your error, but my guess is that its similar to
what
you get when java reflection is used - you need pass the otherwise implicit
"this" argument explicitely. Use __call__(PyObject [] args), where the first
argument is supposed to be an instance of MyClass.

Diez




-----Original Message-----
From: Kay
To: jython-users@[...].net
Sent: 10/12/2003 5:58 PM
Subject: [Jython-users] call a java-method from embedded interpreter


Hello!

I want to start the method "run()" (with an embedded interpreter)
from 'MyClass.java', but i'm getting the following exception:

--> 
Traceback (innermost last):   (no code object) at line 0 TypeError:
run():
expected 1 args; got 0 <--

... dir() shows "obj1";
The InteractiveConsole starts 'run' without problems!
Do you know, where is the mistake?

Thx!

Regards,
Kay


-=>  App.java <=-
:

Object obj;
:
interp = new PythonInterpreter();
interp.set("obj1", obj);
PyObject pyObject = interp.eval("obj1");
pyObject.__getattr__("run").__call__(); <--- doesn't work?!

:



-=>  MyClass.java <=-

public class MyClass implements Runnable{

    public JFrame frame = new JFrame();


    public void run() {
      System.out.println("Starting...");
      frame.setSize(200,200);
      frame.show();
    }
}

********************************************************************




-------------------------------------------------------
This SF.net email is sponsored by: SF.net Giveback Program.
SourceForge.net hosts over 70,000 Open Source Projects.
See the people who have HELPED US provide better services:
Click here: http://sourceforge.net/supporters.php
_______________________________________________
Jython-users mailing list
Jython-users@[...].net
https://lists.sourceforge.net/lists/listinfo/jython-users





-------------------------------------------------------
This SF.net email is sponsored by: SF.net Giveback Program.
SourceForge.net hosts over 70,000 Open Source Projects.
See the people who have HELPED US provide better services:
Click here: http://sourceforge.net/supporters.php
_______________________________________________
Jython-users mailing list
Jython-users@[...].net
https://lists.sourceforge.net/lists/listinfo/jython-users
Thread:
Jeff Emanuel
Kay
Diez B. Roggisch

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved