how to call java methods in python

M

masood shaik

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.
 
A

alex23

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?
 
M

masood shaik

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.
 
C

Chris Rebert

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
 
M

masood shaik

Please help me

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

Laszlo Nagy

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
 
S

Steven D'Aprano

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
 
A

Alan Meyer

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top