how to call java methods in python

Discussion in 'Python' started by masood shaik, Oct 4, 2011.

  1. masood shaik

    masood shaik Guest

    Hi

    I am trying to import java method in python. But i am getting the
    following error.


    error: ImportError: No module named Calculator


    Calculator is the name of the class written in java. I am trying to
    access the methods of that class.

    Please help me.
     
    masood shaik, Oct 4, 2011
    #1
    1. Advertising

  2. masood shaik

    alex23 Guest

    On Oct 4, 4:39 pm, masood shaik <> 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?
     
    alex23, Oct 4, 2011
    #2
    1. Advertising

  3. masood shaik

    masood shaik Guest

    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 <> wrote:
    > On Oct 4, 4:39 pm, masood shaik <> 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?
     
    masood shaik, Oct 4, 2011
    #3
  4. masood shaik

    Chris Rebert Guest

    On Tue, Oct 4, 2011 at 12:14 AM, masood shaik <> 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
     
    Chris Rebert, Oct 4, 2011
    #4
  5. masood shaik

    masood shaik Guest

    On Oct 4, 1:16 pm, Chris Rebert <> wrote:
    > On Tue, Oct 4, 2011 at 12:14 AM, masood shaik <> 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

    >


    Please help me

    Regarding how to create java bridge as how to call a java function
    from python


    > > 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
     
    masood shaik, Oct 4, 2011
    #5
  6. masood shaik

    Laszlo Nagy Guest


    > Please help me
    >
    > Regarding how to create java bridge as how to call a java function
    > from python

    If you want to call a Java *method* from the C python implementation,
    then there are many possibilities. For example, you can write your Java
    program as a service. The Python program can talk to it through some
    kind of IPC mechanism. There are lots of ways to do communication
    between two running processes.

    Lower lever would be (just an example) to open a TCP/IP server socket on
    the Java side, and connect to it from the Python side. Then develop your
    own protocol for communication.

    Higher level would be (just another example) to use CORBA/ORB on the
    Java side, and do the same from the Python side.

    There are dozens of libraries that can help you communicate between
    processes. I suggest that you look after these libraries, and select one
    that suits your needs.

    If you want to call Java methods that run inside the same process with
    Python then I have bad news. This won't be possible. (Or it might be,
    but don't worth the work...)

    Best,

    Laszlo
     
    Laszlo Nagy, Oct 4, 2011
    #6
  7. On Tue, 04 Oct 2011 00:14:49 -0700, masood shaik wrote:

    > Hi
    > I am trying out my hand on accessing java methods in python. here is
    > my piece of code..

    [...]
    > 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.



    Jython *is* Python. It is a Python implementation built on the Java
    virtual machine. Integrating Java with Python is a major reason for using
    Jython.

    http://www.jython.org/

    You can't just import a Java library into the C implementation of Python,
    any more than you can just import a Python library into Java. You need an
    interface between them. Jython includes one such interface. The C
    reference implementation of Python does not.

    To integrate Java libraries with CPython is harder, but not impossible. I
    have never tried any of these, but you could try JPype, Py4J, or JCC:

    http://jpype.sourceforge.net/
    http://py4j.sourceforge.net/index.html
    http://pypi.python.org/pypi/JCC/2.1



    --
    Steven
     
    Steven D'Aprano, Oct 5, 2011
    #7
  8. masood shaik

    Alan Meyer Guest

    On 10/4/2011 2:39 AM, masood shaik wrote:
    > Hi
    >
    > I am trying to import java method in python. But i am getting the
    > following error.
    >
    >
    > error: ImportError: No module named Calculator
    >
    >
    > Calculator is the name of the class written in java. I am trying to
    > access the methods of that class.
    >
    > Please help me.


    Masood,

    What you are trying to do requires a lot of understanding of advanced
    computer programming. I suggest you read Laszlo's response, and the
    links suggested by Steven. If you find those responses too hard to
    understand, or if you don't understand the difference between Jython and
    CPython, or if you don't know what CPython is, then you are "in over
    your head" as we say in the U.S. You are trying to do something that is
    too hard for you.

    If that is so, and if this is for a school project, I suggest you talk
    to the teacher about getting an easier project.

    Good luck.

    Alan
     
    Alan Meyer, Oct 5, 2011
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. C B
    Replies:
    1
    Views:
    454
  2. Rajesh
    Replies:
    3
    Views:
    1,493
    Oliver Wong
    Jul 13, 2006
  3. Shashank Agarwal
    Replies:
    2
    Views:
    164
    Tiago Macedo
    Jun 30, 2008
  4. Kenneth McDonald
    Replies:
    5
    Views:
    379
    Kenneth McDonald
    Sep 26, 2008
  5. Replies:
    0
    Views:
    473
Loading...

Share This Page