Mailing List Archive

Jpython Question
I had a quick question on Jpython and I'm not sure if this is the right
place to post, but I figured I would try anyway...

I was just wondering if jpython can instantiate java classes and invokes
methods on them...any help would be welcome...

Thanks,

Ian
Jpython Question [ In reply to ]
>I had a quick question on Jpython and I'm not sure if this is the right
>place to post, but I figured I would try anyway...


You might want to subscribe to jpython-interest mailing list. (See
www.jpython.org for subscription information)

>I was just wondering if jpython can instantiate java classes and invokes
>methods on them...any help would be welcome...


Yes and yes. I'm always amazed by just how well the Java/Python integration
works in JPython (Kudos to Jim and Barry).

-tim
Jpython Question [ In reply to ]
* Ian Kezsbom
|
| I was just wondering if jpython can instantiate java classes and
| invokes methods on them...any help would be welcome...

It can, and it's very easy too. Here's a snippet from an email I sent
earlier today:

C:\Mine dokumenter>jpython
JPython 1.1beta1 on java1.1.7B
Copyright (C) 1997-1999 Corporation for National Research Initiatives
>>> from java.util import Hashtable
>>> h=Hashtable()
>>> h.put("larsga@ifi.uio.no","Lars Marius Garshol")
>>> h.get("larsga@ifi.uio.no")
'Lars Marius Garshol'
>>>

It's actually as easy as this.

--Lars M.
Jpython Question [ In reply to ]
Ian Kezsbom wrote:

> I was just wondering if jpython can instantiate java classes and invokes
> methods on them...any help would be welcome...

Yes. Furthermore, you can extend Java classes in JPython, and override and
add methods, or implement
Java interfaces. You can pass back objects of these JPython classes to Java methods
that accept arguments of the base type. It's a remarkably well-done two-way
integration.

-- Randy Hudson
Jpython Question [ In reply to ]
Ian Kezsbom wrote in message ...
>Just out of curiosity...lets say I've written a Java program, with many
>classes and I want to run that program through JPython and have access to
>all its classes...how would I go about doing this...


Its almost too easy to describe. I really recomend you just try it out.
However, let's say you have a java class Triangle in the default java
package. After compiling Triangle with your favorite java compiler (e.g.,
javac) you can import the class directly.

# For example
import Triangle
triangle = Triangle()
print = triangle.area()

More likely, you'd have your java code in a package. If you're java class
triangle is in the package shapes you'd do something like:

from shapes import Triangle
# ....

Note that the java class files must be on your classpath. If the class files
are the current directory, this works automatically. Otherwise you're going
to have to modify the classpath in the jpython launcher (called either
"jpython" or "jpython.bat").

-tim
Jpython Question [ In reply to ]
Just out of curiosity...lets say I've written a Java program, with many
classes and I want to run that program through JPython and have access to
all its classes...how would I go about doing this...

Ian

On 20 Jul 1999, Lars Marius Garshol wrote:

>
> * Ian Kezsbom
> |
> | I was just wondering if jpython can instantiate java classes and
> | invokes methods on them...any help would be welcome...
>
> It can, and it's very easy too. Here's a snippet from an email I sent
> earlier today:
>
> C:\Mine dokumenter>jpython
> JPython 1.1beta1 on java1.1.7B
> Copyright (C) 1997-1999 Corporation for National Research Initiatives
> >>> from java.util import Hashtable
> >>> h=Hashtable()
> >>> h.put("larsga@ifi.uio.no","Lars Marius Garshol")
> >>> h.get("larsga@ifi.uio.no")
> 'Lars Marius Garshol'
> >>>
>
> It's actually as easy as this.
>
> --Lars M.
>
>
Jpython Question [ In reply to ]
Ian Kezsbom wrote:
>
> Just out of curiosity...lets say I've written a Java program, with many
> classes and I want to run that program through JPython and have access to
> all its classes...how would I go about doing this...

I've done this; here's how:

First, define a thread that will become the main thread of the program.

from java.lang import Thread, String
from wherever import MyAppMainClass
from jarray import array # a method to convert a Python list to a Java array
class AppThread(Thread): # defines a class AppThread that extends java.lang.Thread
def __init__(self,args): # define the AppThread constructor
# This example takes the args list for the
# call to main as a constructor
# "self" is the conventional ID for Java "this"
self.args = args
def run(self): # override Thread.run()
MyAppMainClass.main( array(self.args,String) )

At this point we've define a class AppThread to run the program on another
thread. Create an instance of this thread, with the appropriate "command line"
arguments:

appThread = AppThread(["-runSilent", "-runDeep"])

If you need to set any Java properties, do it now:

from java.lang import System
props = System.getProperties()
props['maxDepth'] = '20000' # like a -DmaxDepth=20000 switch to jre

Now start the program

appThread.start() # standard Thread.start(), which calls the run() defined above

At this point you can look at anything publicly accessible

from wherever import InstrumentPanel
nicely_display( InstrumentPanel.getDepth() )

-- Randy Hudson