Tuesday, January 13, 2009

jpype usage " absolutely working " turorial

this tutorial tells basic usage of jpype , calling java class with python

suppose java file is this

..................................................................................................................................................................

package com;

class lohi
{
public lohi(String name){}
public static void main(String str[])
{
System.out.print("welcome\n");
}
public void look()
{
System.out.print("Ashish");
}
}
...................................................................................................................................................................

compile this code and add the class file lohi.class is folder "c:\com\" now python code


import jpype
import os.path
jarpath = os.path.join(os.path.abspath('.'), '.')
jpype.startJVM("C:/Program Files/IBM/Java50/jre/bin/classic/jvm.dll", "-ea","-Djava.class.path=C:/")
testPkg = jpype.JPackage('com')
lohith=testPkg.lohi
l=lohith("ashish")
l.look()
jpype.shutdownJVM()


run this code and it will work


you have to change "C:/Program Files/IBM/Java50/jre/bin/classic/jvm.dll" to your system jvm path

......................................................................................................................................................................
after facing lot of errors i got this code working . and doubts please feel free to mail me lookforlohith@yahoo.co.in

No comments: