Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > how to call java methods in python

Reply
Thread Tools

how to call java methods in python

 
 
masood shaik
Guest
Posts: n/a
 
      10-04-2011
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.





 
Reply With Quote
 
 
 
 
alex23
Guest
Posts: n/a
 
      10-04-2011
On Oct 4, 4:39*pm, masood shaik <(E-Mail Removed)> 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?

 
Reply With Quote
 
 
 
 
masood shaik
Guest
Posts: n/a
 
      10-04-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 <(E-Mail Removed)> wrote:
> On Oct 4, 4:39*pm, masood shaik <(E-Mail Removed)> 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?


 
Reply With Quote
 
Chris Rebert
Guest
Posts: n/a
 
      10-04-2011
On Tue, Oct 4, 2011 at 12:14 AM, masood shaik <(E-Mail Removed)> 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
 
Reply With Quote
 
masood shaik
Guest
Posts: n/a
 
      10-04-2011
On Oct 4, 1:16*pm, Chris Rebert <(E-Mail Removed)> wrote:
> On Tue, Oct 4, 2011 at 12:14 AM, masood shaik <(E-Mail Removed)> 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


 
Reply With Quote
 
Laszlo Nagy
Guest
Posts: n/a
 
      10-04-2011

> 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



 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      10-05-2011
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
 
Reply With Quote
 
Alan Meyer
Guest
Posts: n/a
 
      10-05-2011
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Is there a way to find the class methods of a class, just like'methods' finds the instance methods? Kenneth McDonald Ruby 5 09-26-2008 03:09 PM
rjb heap size OR call ruby methods from java Shashank Agarwal Ruby 2 06-30-2008 02:54 PM
How to Call Java Methods from inside XSLT Rajesh Java 3 07-13-2006 06:09 PM
simple language interpreter to call Java methods (rules engines) C B Java 1 09-08-2004 11:18 PM



Advertisments