Jython inherit from Java class

Kent Johnson kent at kentsjohnson.com
Wed Feb 8 22:18:48 EST 2006


Mark Fink wrote:
> I wrote a Jython class that inherits from a Java class and (thats the
> plan) overrides one method. Everything should stay the same.
> 
> If I run this nothing happens whereas if I run the Java class it says:
> usage: java fit.FitServer [-v] host port socketTicket
>         -v      verbose

It sounds like the Java class has a main() function. When you run it as 
an application, the Java runtime looks for the main() function in the 
class you tell it to run.

Even in Java, this behaviour is not preserved by inheritance, a subclass 
has to have its own main() to be executable.

Jython (and Python) works a little differently, your module is executed 
from start to finish when you run it. Any statements that are not inside 
a class or function definition will be executed. (Actually the class and 
function definitions are executed to, but not in the same way...)

You might get the behaviour you want if you put the line
import sys
fit.FitServer.main(sys.argv)

at the end of your file. This will run the main() function of the Java 
class.

Kent

> 
> I think this is because I do not understand the jython mechanism for
> inheritance (yet).
> 
> JyFitServer.py:
> ===========
> import fit.FitServer
> import fitnesse.components.FitProtocol
> from fit.Parse import Parse
> from fit.Fixture import Fixture
> 
> # Inherit from original Java FitServer Implementation by Robert C.
> Martin and Micah D. Martin
> class FitServer(fit.FitServer):
>     # call constructor of superclass
>     def __init__(self, host, port, verbose):
>         FitServer.__init__(self, host, port, verbose)
> 
>     # override process method
>     def process(self):
>         self.fixture.listener = self.fixtureListener
>         print "hello, I am JyFitServer!"
>         try:
>             size = FitProtocol.readSize(self.socketReader)
>             if size > 0:
>                 try:
>                     document =
> FitProtocol.readDocument(self.socketReader, size)
>                     tables = Parse(document)
>                     fixture = Fixture()
>                     fixture.listener = self.fixtureListener;
>                     fixture.doTables(tables)
>                     self.counts.tally(self.fixture.counts)
>                 except FitParseException, e:
>                     self.exception(e)
>         except Exception, e:
>             self.exception(e)
> 
> 
> 
> Please help,
> Mark Fink
> 



More information about the Python-list mailing list