aspose file tools*
The moose likes Jython/Python and the fly likes How to call a Python method form Java class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Languages » Jython/Python
Bookmark "How to call a Python method form Java class" Watch "How to call a Python method form Java class" New topic
Author

How to call a Python method form Java class

eswari Malluru
Greenhorn

Joined: Jul 07, 2008
Posts: 7
Hi All,

I new to python and want to call python script from java class. For this using python interpreter and able to do so. But want to call a particular python method from java class.

Python script using is as show below:


#processing.py

import os
import datetime

class Processing(object):
"""
Processing class.
"""
def __init__(self, gui):
self.gui = gui
self.numbers = None
self.vendor = None
self.raNumber = None
self.comPort = None
self.logFile = None
self.thread = None
self.results = None
self.testSuite = None
self.retryEvent = None

def run(self,logFile,comPort,numbers,vendor,raNumber,testSuite,SerialErrorEvent,ProcessingErrorEvent):
"""
"""
self.logFile = logFile
self.comPort = comPort
self.serialNumbers = serialNumbers
self.vendor = vendor
self.raNumber = raNumber
self.SerialErrorEvent = SerialErrorEvent
self.ProcessingErrorEvent = ProcessingErrorEvent

try:
................
...........


and my java class is as below:

PythonInterpreter.initialize(System.getProperties(), System.getProperties(), new String[0]);
PythonInterpreter interp = new PythonInterpreter();

System.out.println();
interp.execfile("E:\\processing.py");


Now want to call the method run from java class. Please tell me how to do this with a sample code


Thanks,
Eshwari
Matt Cartwright
Ranch Hand

Joined: Aug 25, 2008
Posts: 149

here you go...

Java:


Python


HTH
Matt
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to call a Python method form Java class