Test.py
import sys
class Test :
def main(self):
args = sys.argv
option = args[1]
print 'Hello ' + option
edit = Test()
edit.main()
Run the jython file using:
jython Test.py World
The Result would be:
Hello World
Monday, October 26, 2009
Use Java API in jython
test.properties
abc=Hello World
Test.py
import java
import java.util as util
import java.io as javaio
class EditProperties :
prop = None
PROPERTIES_FILE = None
def __init__ (self):
self.PROPERTIES_FILE = 'test.properties'
self.prop = util.Properties()
try :
propfile = javaio.FileInputStream(self.PROPERTIES_FILE)
self.prop.load(propfile)
except IOError, ioe:
print "Exception in properties file -", ioe
def main(self):
try :
fr = open(self.PROPERTIES_FILE, 'r')
ar = java.util.ArrayList()
self.readfile(fr, ar)
fw = open(self.PROPERTIES_FILE, 'w')
self.writefile(fw, ar)
except IOError, ioe:
print "Exception in file -", ioe
def readfile(self, fr, ar) :
temp = None
inLines = fr.readlines()
print self.prop.getProperty('abc')
for inLine in inLines :
print inLine
ar.add(inLine)
def writefile(self, fw, ar) :
ar.add('\n')
ar.add('xyz=Hello Country')
iter = ar.iterator()
while iter.hasNext() :
fw.write(iter.next())
edit = EditProperties()
edit.main()
abc=Hello World
Test.py
import java
import java.util as util
import java.io as javaio
class EditProperties :
prop = None
PROPERTIES_FILE = None
def __init__ (self):
self.PROPERTIES_FILE = 'test.properties'
self.prop = util.Properties()
try :
propfile = javaio.FileInputStream(self.PROPERTIES_FILE)
self.prop.load(propfile)
except IOError, ioe:
print "Exception in properties file -", ioe
def main(self):
try :
fr = open(self.PROPERTIES_FILE, 'r')
ar = java.util.ArrayList()
self.readfile(fr, ar)
fw = open(self.PROPERTIES_FILE, 'w')
self.writefile(fw, ar)
except IOError, ioe:
print "Exception in file -", ioe
def readfile(self, fr, ar) :
temp = None
inLines = fr.readlines()
print self.prop.getProperty('abc')
for inLine in inLines :
print inLine
ar.add(inLine)
def writefile(self, fw, ar) :
ar.add('\n')
ar.add('xyz=Hello Country')
iter = ar.iterator()
while iter.hasNext() :
fw.write(iter.next())
edit = EditProperties()
edit.main()
Use your java class in jython
Addtion.java:
package calc;
public class Addition {
public int add(int a, int b) {
return a + b;
}
}
Test.py:
import calc as ad
class Test:
add = ad.Addition()
print add.add(10, 20)
Test()
package calc;
public class Addition {
public int add(int a, int b) {
return a + b;
}
}
Test.py:
import calc as ad
class Test:
add = ad.Addition()
print add.add(10, 20)
Test()
Subscribe to:
Posts (Atom)