do you need jython for calling a python script from java? - java

I have seen in answered here that if you want to call a python script from java, you can use jython as a option.
But have seen other answers as well saying, you can use Process or ProcessBuilder and call their exec or start methods to run the python script.
As I understand jython allows you to program python in you java code, but it allows you to call python scripts as well via, PythonInterpreter.execfile.
So I'm wondering what are my options if I want to call a python script (e.g. text processing script which uses nltk) from my java code and along with some arguments and return the results back to my java programme? which option should I use?

Jython is an implementation of the Python language for the Java platform
(source)
You can simply use ProcessBuilder if there is a regular python script on a machine that has regular python installed. This saves you from having to include a whole python runtime in your java application. You can pass parameters quite easily, the returned text is a little trickier: See http://examples.javacodegeeks.com/core-java/lang/processbuilder/java-lang-processbuilder-example/ or other examples. Basically anything that the python program prints can be captured that way.
However if you don't like external dependencies and prefer shipping everything in a single application you can try jython. It has some limitations though since it doesn't come with all those modules and it seems it may have difficulties with C modules. Getting Python's nltk.wordnet module working for Jython looks like an explanation on how to get nltk to run with it. Arguments and return values should be simpler with jython since the integration is better. And if you want more than just printed text, Jython can even call into java code directly from python. That would give you interesting options for hybrid code.

Related

How to use NLTK in Jython, or PY4J cross platforn?

I have two questions, this could go into different directions based on opinion, but as of right now I am writing a Java/Kotlin API. So far it is compatible across all platforms, excluding IOS(have not tested).
A few of the tasks it runs is python using PY4J but the way the API calls the python script to start the PY4J connection is
Runtime.getRuntime().exec("python script.py") # Is there a better way to do this?
Which is fine until it comes to Android. Will Android be able to use this method? If so, the question is answered and finished there.
If not, is there a way to embed a python interpreter into a JAR file? Java project?
I like Jython, but I could not get NLTK to work with it.
1) Is there a way to make Jython work with NLTK : If not,
2) Is there a way to use PY4J in Python?
3) Is there a way to embed a custom Python interpreter in Java/JAR?
Feel free to edit my question, code and title. I don't do this much, so it is probably sloppy.
This was silly, but I will keep it up since this was a real question. You can install python3 directly into the JAR. When the python installation screen shows up you can just install it into a folder inside your Java project.
For the Android part, there is a python package called p4a, python-for-android. You can create a python interpreter with dependencies. This should work.

How do you call C++ and\or Java functions from Python 2.7?

I am creating a Windows program that so far has a .bat file calling a .pyw file, and I need functions from Java and C++. How can I do this?(I don't mind creating a new batch or python file, and I already have the header file for the C++ section and a .jar file for my java components. (For Java I use Eclipse Java Mars, and it's Java 8u101)) Thanks!!!
This is rather simple for C++: you have to compile a library with the function, import it in Python and... call it! Python has a powerful ctypes module in the standard library to handle this kind of tasks.
Here is an example of loading print() function from hypothetical libc.dll.
from ctypes import *
libc = cdll.LoadLibrary("libc.dll")
>>> print(libc.time(None))
1150640792
Calling Java from Python is covered here: How to call a java function from python/numpy?
You can load C++ function and execute it from Python like BasicWolf explained in his answer. For Java, Jython might be a good approach. But then there's a problem - you will need to be dependent on Jython which is not up to date with the latest versions of Python. You will face compatibility issues with different libraries too.
I would recommend compiling your C++ and Java functions to create individual binaries out of them. Then execute these binaries from within Python, passing the arguments as command line parameters. This way you can keep using CPython. You can interoperate with programs written in any language.

Pass a File object to a Python script from Java, then get the outputted result as a new File object

I'm working on a big Java project right now, and I decided that the easiest way to "fix" an issue I have with the data in a File object would be to parse and edit it with a Python script I wrote. I tested the Python script on its own on this particular file (outside of Java) and it works fine. Now I'm just wondering how I integrate my Python script with the rest of my Java code.
I basically want to somehow pass this File object to my Python script, which has to be called from Java. Then I want to take the file that the Python script outputs, read it back in as a File object, and return that from this particular Java method.
From researching a bit, I've heard Jython come up a bit, as well as the built-in Python interpreter in Java 6 and later. I'm just not sure where to start. Or alternatively, I guess I could port this Python script over to Java, but that's kind of a last resort. Any help is appreciated!
Look at these classes
http://docs.oracle.com/javase/7/docs/api/java/lang/Process.html
http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html
You can use them in Java to call your Python process.

Call Java (ImageJ plugin BoneJ) from a Jython script

**Sorry, I am very new to both python and java so I might not recognize that this has been answered and only needs adjusted for my situation.
Essentially I have a script manager that operates in a python environment and I need to pass data off to the bonej plugin for analysis, which should then return an array that will then be used by another python script for further analysis.
I have been directed to jython as a way to do this, but I can't figure out how to import imageJ as if it were a module.
I know that I can simply open imagej from a command line and direct it to analyze an image, but this is not what I need because it simply returns an average in a segment of bone.
Basically I need to tell imagej that it should analyze this segment of bone, return the array, then this next segment of bone. An additional python script interpolates the arrays onto a predefined background grid.
If anyone has some insight on how to achieve this back and fort from jython I would appreciate it.
You can indeed use Jython to call ImageJ. See the Jython scripting page on the Fiji wiki for an overview and lots of example code. One easy way to get started from within ImageJ is to use the Script Editor. You can then invoke your Jython scripts from the command line using the ImageJ launcher [instructions]. Or you can run them via the jython executable by adding the needed Java libraries to the classpath.
But beware: this does not provide total integration between Python libraries and Java ones. The problem is that many Python libraries are backed by C code, making them accessible only from CPython, and not from Jython. For example, the very popular SciPy and NumPy libraries cannot be called directly from Jython code.

Java Wrapper to Perl/Python code

I have to deploy some Web Services on a server that only supports the Java ones, but some of them will be done using perl or python. I want to know if is possible to develop a Java wrapper to call a specific code written in perl or python. So, I want to have all the Web Services in Java, but some of them will call some code using other languages.
Thanks in advance.
Regards,
Ukrania
This depends heavily upon your needs. If Jython is an option for the Python code (it isn't always 100% compatible), then it is probably the best option there. Otherwise, you will need to use Java's Process Builder to call the interpretters directly and return the results on their output stream. This will not be fast (but then again, Jython isn't that fast either, relative to regular Java code), but it is an extremely flexible solution.
For the Python part of it you can use Jython to run Python code right from your Java virtual machine. It'll integrate fully with your Java code as a bonus.
For Perl, use Inline::Java. There are several options for integrating the code; you can call a separate process or you can use an embedded interpreter.
For Python you can use the Java Scripting API.
A Perl implementation is sadly still missing.
There's something I used a while back called Jython which allows you to execute Python code from Java. It was a little quirky, but I got it to do what I needed.
http://www.jython.org

Categories