From 95de9d301e319b05d1ba7cda7c56b77d409eed12 Mon Sep 17 00:00:00 2001 From: Imran M Yousuf Date: Mon, 15 Sep 2008 11:44:41 +0600 Subject: [PATCH] Add Python demo for showing how to embed Python in Java It also shows how to use Jython as a JSR-223 scripting language. Signed-off-by: Imran M Yousuf --- .gitignore | 1 + README | 6 ++ pom.xml | 80 ++++++++++++++++++++++ .../smartitengineering/demo/jython/test/App.java | 45 ++++++++++++ .../demo/jython/test/TestInterface.java | 16 +++++ .../demo/jython/test/TestName.java | 31 +++++++++ src/main/python/InterfaceTest.py | 23 +++++++ .../demo/jython/test/AppTest.java | 38 ++++++++++ 8 files changed, 240 insertions(+) create mode 100644 .gitignore create mode 100644 README create mode 100644 pom.xml create mode 100644 src/main/java/com/smartitengineering/demo/jython/test/App.java create mode 100644 src/main/java/com/smartitengineering/demo/jython/test/TestInterface.java create mode 100644 src/main/java/com/smartitengineering/demo/jython/test/TestName.java create mode 100644 src/main/python/InterfaceTest.py create mode 100644 src/test/java/com/smartitengineering/demo/jython/test/AppTest.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..eb5a316 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +target diff --git a/README b/README new file mode 100644 index 0000000..261621e --- /dev/null +++ b/README @@ -0,0 +1,6 @@ +This is a demo project showing Java and Python integration through Jython. +It also shows the usage of Jython as a scripting language. It uses Jython 2.2.1 + +Use the following command to see it in action: + +mvn clean compile exec:java -Dfile.encoding=UTF-8 diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..61e9fc6 --- /dev/null +++ b/pom.xml @@ -0,0 +1,80 @@ + + + 4.0.0 + com.smartitengineering.demo + jython-test + jar + 1.0-SNAPSHOT + jython-test + http://maven.apache.org + + + junit + junit + 3.8.1 + test + + + jython + jython + 2.2.1 + + + com.sun.script + jython-engine + 1.1 + + + + + maven2-repository.dev.java.net + Java.net Repository for Maven + http://download.java.net/maven/2/ + default + + false + + + + smart-it-snapshot-repo + Smart IT Engineering snapshot repo + http://dev.smartitengineering.com/maven-repo/snapshot + + false + + + true + + + + smart-it-release-repo + Smart IT Engineering release repo + http://dev.smartitengineering.com/maven-repo/release + + true + + + false + + + + + + + org.codehaus.mojo + exec-maven-plugin + + + + java + + + + + com.smartitengineering.demo.jython.test.App + + + + + diff --git a/src/main/java/com/smartitengineering/demo/jython/test/App.java b/src/main/java/com/smartitengineering/demo/jython/test/App.java new file mode 100644 index 0000000..ab0d300 --- /dev/null +++ b/src/main/java/com/smartitengineering/demo/jython/test/App.java @@ -0,0 +1,45 @@ +package com.smartitengineering.demo.jython.test; + +import java.io.FileReader; +import javax.script.ScriptEngine; +import javax.script.ScriptEngineManager; +import org.python.core.Py; +import org.python.core.PyObject; +import org.python.util.PythonInterpreter; + +/** + * Hello world! + * + */ +public class App { + + public static void main(String[] args) + throws Exception { + PythonInterpreter interp = new PythonInterpreter(); + interp.execfile("./src/main/python/InterfaceTest.py"); + PyObject pythonObject; + PyObject returnedObject; + pythonObject = interp.eval("MyTestInterface()"); + returnedObject = pythonObject.invoke("isDivisible", + interp.eval("int(4)"), + interp.eval("int(2)")); + System.out.println(Py.py2boolean(returnedObject)); + TestName name = new TestName("Jobaer Chowdhury"); + System.out.println(name.isRead()); + returnedObject = pythonObject.invoke("printName", Py.java2py(name)); + System.out.println(name.isRead()); + returnedObject = pythonObject.invoke("getName"); + TestName herName = (TestName) Py.tojava(returnedObject, TestName.class.getName()); + System.out.println(herName.getName()); + pythonObject = interp.eval("MyRunnable()"); + Runnable runnable = (Runnable) Py.tojava(pythonObject, Runnable.class.getName()); + Thread thread = new Thread (runnable); + thread.start(); + /** + * The JSR-223 way. Using Jython as a scripting only language. + */ + ScriptEngine engine = new ScriptEngineManager().getEngineByName("python"); + engine.eval(new FileReader("./src/main/python/InterfaceTest.py")); + engine.eval("print \"Dividing 6 by 3, is it divisible: \", MyTestInterface().isDivisible(6,3)"); + } +} diff --git a/src/main/java/com/smartitengineering/demo/jython/test/TestInterface.java b/src/main/java/com/smartitengineering/demo/jython/test/TestInterface.java new file mode 100644 index 0000000..c28e9f4 --- /dev/null +++ b/src/main/java/com/smartitengineering/demo/jython/test/TestInterface.java @@ -0,0 +1,16 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package com.smartitengineering.demo.jython.test; + +/** + * + * @author imyousuf + */ +public interface TestInterface { + public boolean isDivisible(int a, int b); + public void printName(TestName name); + public TestName getName(); +} diff --git a/src/main/java/com/smartitengineering/demo/jython/test/TestName.java b/src/main/java/com/smartitengineering/demo/jython/test/TestName.java new file mode 100644 index 0000000..56cca0c --- /dev/null +++ b/src/main/java/com/smartitengineering/demo/jython/test/TestName.java @@ -0,0 +1,31 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package com.smartitengineering.demo.jython.test; + +/** + * + * @author imyousuf + */ +public class TestName { + private String name; + private boolean read; + + public TestName(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public boolean isRead() { + return read; + } + + public void setRead(boolean read) { + this.read = read; + } +} diff --git a/src/main/python/InterfaceTest.py b/src/main/python/InterfaceTest.py new file mode 100644 index 0000000..71025fe --- /dev/null +++ b/src/main/python/InterfaceTest.py @@ -0,0 +1,23 @@ +import java, time +from com.smartitengineering.demo.jython.test import TestInterface, TestName +from java.lang import Runnable + +class MyTestInterface (TestInterface): + def isDivisible (self, a, b): + c = a % b + if c == 0 : + return bool("true") + else : + return bool("false") + + def printName(self, name): + print name.getName() + name.setRead(bool("true")) + def getName(self): + testName = TestName("Farhana Kabir") + return testName + +class MyRunnable (Runnable): + def run(self): + print "This is a Runnable Thread without compilation" + print "Time: ", time.time() diff --git a/src/test/java/com/smartitengineering/demo/jython/test/AppTest.java b/src/test/java/com/smartitengineering/demo/jython/test/AppTest.java new file mode 100644 index 0000000..6df2a29 --- /dev/null +++ b/src/test/java/com/smartitengineering/demo/jython/test/AppTest.java @@ -0,0 +1,38 @@ +package com.smartitengineering.demo.jython.test; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Unit test for simple App. + */ +public class AppTest + extends TestCase +{ + /** + * Create the test case + * + * @param testName name of the test case + */ + public AppTest( String testName ) + { + super( testName ); + } + + /** + * @return the suite of tests being tested + */ + public static Test suite() + { + return new TestSuite( AppTest.class ); + } + + /** + * Rigourous Test :-) + */ + public void testApp() + { + assertTrue( true ); + } +} -- 2.11.4.GIT