Accessing java objects in python scripts?

newscache3.freenet.de fredrik.thernelius at hotsip.com
Tue Feb 27 05:28:25 EST 2001


*** post for free via your newsreader at post.newsfeeds.com ***

I'm sorry if this subject has been discussed before. If it has then please
give me a reference to that discussion.

I'll now give a short description of my problem:

In my java program I have instaciated a javaobject of a certain kind, lets
call the class Testclass and new instanciated object testObject. I want to
access this testObject from my jython interpreter. What is the best way to
do this?



By using the "try and fail" concept, I have found out that there are a
couple of way to do this. In order to hava a "pretty" solution I want to
have a python class that will be able to use methods from the testClass
object. In fact, I can't even find this testObject in ANY namespace. Please
find it for me!



Notice that I've not bothered to add "import" statements and some other
stuff in the code.



Regards,

Fredrik Thernelius



Java program:

-------------

python = new PythonInterpreter();

Testclass testObject = new TestClass();   //create a java object

python.exec("e = Engine()");              //create a python object

python.set("obj", testObject);            //making the object accessable in
python

python.exec("e.runEngine()");

//python.set("r.obj", testObject);

python.exec("print obj.toString()");      //Here I can access the
testObject.

python.exec("print globals()");   //<-----in this name space I can see my
testObject





Python script:

--------------

class Engine:

      def __init__(self):

            try:

                  self.compiler =  Perl5Compiler()

                  self.matcher =   Perl5Matcher()



                  #I can't find the testObject

                  print "\nprinting local  variables : ", locals().keys()

                  print "\nprinting global variables: ", globals().keys()

                  print "\nprinting __dict__  variables: ",
self.__dict__.keys()

                  print "\nprinting vars() variables: ", vars()

                  print "\nprinting dir() variables: ", dir()



            except:

                  print "Unexpected Error:", sys.exec_info()

                  raise

            return





      def runEngine(self):

            try:

                  print self.obj.toString()         #<--- this does not
work!

                  #print obj.toString()              <--- this does not work
either!

            except:

                  print "Runtime Error!"

                  raise

            return










  **** Post for FREE via your newsreader at post.newsfeeds.com ****

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
*** Newsfeeds.com - The #1 Usenet Newsgroup Service on The Planet! ***
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  http://www.newsfeeds.com       |         http://www.newsfeeds.com
                                 |
* Anonymous posting server!      | * Totally Uncensored!
* SUPER Servers!                 | * Over 80,000 Newsgroups!
* BINARIES ONLY Servers!         | * 16 seperate Newsgroup Servers!
* SPAM FILTERED Server!          | * Instant access!
* ADULT ONLY Server!             | * Multiple OC 3's and OC 12's!
* MP3 ONLY Server!               | * 99% Article Completion!
* MULTIMEDIA ONLY Server!        | * Months of Retention!
* 7 UNCENSORED Newsgroup Servers | * Lightning FAST downloads!
                                 |
  http://www.newsfeeds.com       |         http://www.newsfeeds.com
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
        **** Point your newsreader to post.newsfeeds.com ****
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= 



More information about the Python-list mailing list