how to call java methods in python

masood shaik masood.524 at gmail.com
Tue Oct 4 03:14:49 EDT 2011


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.
Please do help me.

On Oct 4, 11:52 am, alex23 <wuwe... at gmail.com> wrote:
> On Oct 4, 4:39 pm, masood shaik <masood.... at gmail.com> wrote:
>
> > Please help me.
>
> Please help us help you. You've given us nothing but an error message.
> (Which seems to indicate that you're trying 'import Calculator'...)
>
> What are you using to call Java methods in Python?
>
> Can you provide a small example of code that demonstrates the problem?




More information about the Python-list mailing list