how to call java methods in python
Chris Rebert
clp2 at rebertia.com
Tue Oct 4 04:16:43 EDT 2011
On Tue, Oct 4, 2011 at 12:14 AM, masood shaik <masood.524 at gmail.com> wrote:
> Hi
> I am trying out my hand on accessing java methods in python. here is
> my piece of code..
>
> Calculator.java
> ---------------
> public class Calculator {
>
> public Calculator(){
>
> }
>
> public double calculateTip(double cost, double tipPercentage){
> return cost * tipPercentage;
> }
>
> public double calculateTax(double cost, double taxPercentage){
> return cost * taxPercentage;
> }
>
> }
>
> javaInPython.py
> ---------------
> import Calculator
>
> class javaInPython(Calculator):
> def __init__(self):
> pass
>
> def calculateTotal(self, cost, tip, tax):
> return cost + self.calculateTip(tip,tax) +
> self.calculateTax(tax,tip)
>
> if __name__ == "__main__":
> calc = javaInPython()
> cost = 23.75
> tip = .15
> tax = .07
> print "Starting Cost: ", cost
> print "Tip Percentage: ", tip
> print "Tax Percentage: ", tax
>
> Now i am trying to access the calculateTip() and its showing import
> error.
> It works fine when i am running it with jython but why its not
> happening in python.
That's because the integration functionality you're depending upon is
*specific to Jython*. Unlike Jython, CPython does not include an
automatic Java bridge. CPython doesn't feature Java integration; only
Jython does.
(N.B.: CPython is the reference implementation of Python, hosted at python.org.)
Cheers,
Chris
More information about the Python-list
mailing list