-
Notifications
You must be signed in to change notification settings - Fork 152
Getting Started
Nate Jensen edited this page Aug 30, 2019
·
19 revisions
Since Jep 3.0
Jep requires that the following dependencies be installed before it can be built and run:
- JDK >= 1.7
- Python = 2.7, 3.3, 3.4, 3.5, 3.6, 3.7, or 3.8
If you cloned or downloaded the Jep source, you will need to build Jep. Simply run
python setup.py build
If the build succeeds it will create a directory jep/build which will contain a jep.jar and the compiled C library of Jep, typically named jep.so or jep.dll depending on your platform.
There are multiple ways to install Jep, in order of least involved to most involved:
- If you used pip install jep, Jep should already be built and installed.
- If you built the source yourself, you can run python setup.py install to install Jep to the standard dirs.
- If you would like to include jep as part of your application, you can place the files as necessary presuming the following conditions are met:
- The jep.jar is accessible to the Java classloaders (typically through the Java classpath)
- The shared library (jep.so or jep.dll) is accessible by the Java process (typically through -Djava.library.path or the environment variable LD_LIBRARY_PATH)
- The jep python files (console.py, java_import_hook.py, version.py, etc) are accessible by Python (typically by placing them in the site-packages/jep directory).
Using Jep in your application is designed to be easy to intermix Java and Python objects in the Python interpreter.
Hello World
try (Interpreter interp = new SharedInterpreter()) {
interp.exec("from java.lang import System");
interp.exec("s = 'Hello World'");
interp.exec("System.out.println(s)");
interp.exec("print(s)");
interp.exec("print(s[1:-1])");
}
Calling Python methods from Java and getting results
try (Interpreter interp = new SharedInterpreter()) {
interp.exec("import somePyModule");
// any of the following work, these are just pseudo-examples
// using exec(String) to invoke methods
interp.set("arg", obj);
interp.exec("x = somePyModule.foo1(arg)");
Object result1 = interp.getValue("x");
// using getValue(String) to invoke methods
Object result2 = interp.getValue("somePyModule.foo2()");
// using invoke to invoke methods
interp.exec("foo3 = somePyModule.foo3")
Object result3 = interp.invoke("foo3", obj);
}
Calling Java constructors from Python
# importing the java.lang.Class objects
from java.util import HashMap
from java.util import ArrayList as AL
# instantiation
x = HashMap()
y = HashMap(100)
a = AL()
Calling Java methods from Python
from java.util import ArrayList, HashMap
a = ArrayList()
a.add("abc")
a += ["def"]
print(a)
m = HashMap()
m.put("listkey", a)
m["otherkey"] = "xyz"
print(m)