Java Python without interpreter - java

So I was looking at various packages for for Java that allow you to run python code via Java. Jython does not handle the equivalent of python 3.6 code. So basically, I have code written in python 3.6 and requires so. I need to be able to utilize that python code via Java without having a python interpreter. Is there such a package? (Note I will not be changing the python code because that is a framework and that traditionally be used in python.
I have a python framework that would be traditionally utilized via python code of course, for python 3.6. Instead, I would like to be utilize it via Java 8 code. Now I looked into Jython but it does not handle 3.6 or 3.x for that matter. I will not be changing the python code from the framework. Additionally, the Java Package should be able to run the python code without an interpreter. Is there such a thing? Py4j requires a python interpreter.

GraalVM compiles Python code to Java bytecode and runs it on the JVM using graalpython, with this caveat:
This Python implementation currently aims to be compatible with Python 3.7, but it is a long way from there, and it is very likely that any Python program that requires any imports at all will hit something unsupported. At this point, the Python implementation is made available for experimentation and curious end-users.

No, running Python code requires some Python interpreter or other.

Since you won't change it, you could convert the Python to an executable and use Java to spawn it as a new process.
ProcessBuilder pb = new ProcessBuilder("C:\\...\\file.exe", command arguments, ..., ...);
pb.start();
You could use File I/O to communicate between Python and Java as the easiest solution.

Related

calling python from java

I need to call some Python code from Java.
I am aware though that Jython exists or that Java has its own Python interpreter.
Thing is, said Python code uses some native (C) compiled code, even runs something on GPU.
So I can divide this into two questions:
Does Jython or Java's Python interpreter support this? That is, the Python code (when called from Java) would run with all the C and GPU parts, thus will run roughly as fast as it would on its own.
If neither of the above mentioned ways support this, is there any other way to do this?
P.S. If that helps, although I suppose it's irrelevant, the code I need to run are highly GPU/C optimized neural networks from Keras (with TensorFlow underneath). I absolutely can't afford to run the only-Python interpreted version.
Currently jython does not support native compiled python modules. In order to run native modules you will need access to a native python(cpython) interpreter from java. There are several open source projects that use JNI to access a cpython interpreter. Three projects that you can look into are JEP, JPY, and JyNI. In regards to GPU access, I only have experience with JEP which I have used with PyCUDA to execute code on the GPU. While I don't have personal experience with tensorflow, I know there are posts on the JEP mailing list regarding using JEP and tensorflow so I believe there are other projects using this combination successfully.

Call C# mono code from java under linux

I have somoe C# source code that I want to be available for java applications under linux OS.
How can I call some C# method from java code?
I have found http://www.mono-project.com/Java but this looks like writing java code in .net environment. It is not what I look for. Rather I need to create new java library that will expose all functionality from C# code, the new library must be executed in pure java environment. Maybe that is possible with IKVM I am not sure.
Use Mono to compile your C# classes on the Linux platform of your choice;
Use JNI to write a set of facade classes between your java code and the compiled C# code.
The closest tool I can find that might be useful would be JNI4Net
I think you would still need Mono to run a .net framework on Linux as well.
Your question is going to be closed (I voted too) simply because it would finally lead to a tool recommendation.
Your best approach is to rewrite them in Java. Or alternatively, there are existing tools (commercial or free) to convert C# to Java,
CSharpJavaMerger Framework
RemObjects C# who compiles C# to JVM.
and many others
Mono is not something you should consider at this moment. Yes, IKVM.NET only helps running Java code on Mono/.NET, and it won't help you in your case.
It's not clear if you can execute the c# project or not through mono, if you can, then process intercomunication is the way to go.
If you are doing it in Linux I should use a pipe channeling to intercomunicate both processes, works really well and is easy to use.
If you can't execute the c# process, then that's another history, you cannot call directly a .net assembly from java.
And also, using mono on Linux per today gives great results (except for ASP .net), we are using it and are getting really good results (faster responses than Java in most scenarios).

Running Python file in Java program

I'm on a semantic project that needs to tokenize the given sentence into words. My opinion is that can be done very easily in python. But I'm familiar with the Jena Framework which is for Java. So I need any way to run python file in java and channelize the outputs to variables in java.
Thanks in Advance.
Take a look at Jython.
Jython is an implementation of Python written in pure Java, and seamlessly integrated with the Java platform. It thus allows you to run Python on any Java platform.

Calling Java from Python

I have a Java app that takes a long time to be initialized (so I can't use a command-line like interface) and I need to pass text and receive the output of a Java method from Python. Is it possible to load the Java application, have it open all the time the Python script runs and use a method from that app?
I don't think the use of Python helps all that much over a command line (at least not a *nix command line), but the basic idea is to communicate over a socket or some similar mechanism. That means that the Java application will have to be wrapped in some code which opens a socket and waits for the python script to contact it. If you are most comfortable with python, you could look to implement that wrapper in Jython.
I've used JPype for something similar and it worked great.
JPype is an effort to allow python programs full access to java class libraries. This is achieved not through re-implementing Python, as Jython/JPython has done, but rather through interfacing at the native level in both Virtual Machines.
If the java app is running you should also consider xml-rpc as it also works well.
Py4J is a way to call Java from a python script. Here is the project website: http://py4j.sourceforge.net/

Using Python from within Java [duplicate]

This question already has answers here:
Closed 13 years ago.
Possible Duplicate:
Java Python Integration
I have a large existing codebase written in 100% Java, but I would like to use Python for some new sections of it. I need to do some text and language processing, and I'd much rather use Python and a library like NLTK to do this.
I'm aware of the Jython project, but it looks like this represents a way to use Java and its libraries from within Python, rather than the other way round - am I wrong about this?
If not, what would be the best method to interface between Java and Python, such that (ideally) I can call a method in Python and have the result returned to Java?
I'm aware of the Jython project, but
it looks like this represents a way to
use Java and its libraries from within
Python, rather than the other way
round - am I wrong about this?
Yes, you are wrong. You can either call a command line interpreter to run python code using Jyton or use python code from Java. In the past there was also a python-to-Java compiler, but it got discontinued with Jython 2.2
I would write a Python module to handle the text and language processing, and then build a small bridge in jython that your java program can interact with. The jython bridge will be a very simple one, that's really only responsible for forwarding calls to the python module, and return the answer from the python module to the java module. Jython is really easy to use, and setup shouldn't take you more than 15 minutes.
Best of luck!
I don't think you could use NLTK from Jython, since it depends on Numpy which isn't ported to the JVM. If you need NLTK or any other native CPython extension, you might consider using some IPC mechanism to communicate between CPython and the JVM. That being said, there is a project to allow calling CPython from Java, called Jepp:
http://jepp.sourceforge.net/
The reverse (calling Java code from CPython) is the goal of JPype and javaclass:
sourceforge.net/projects/jpype/
pypi.python.org/pypi/javaclass/0.1
I've never used any of these project, so I cant't vow for their quality.
Jython is a Python implementation running on the JVM. You can find information about embedding Python in an existing Java app in the user guide.
I don't know the environment that you're working in, but be aware that mixing languages in the same app can quickly lead to a mess. I recommend creating Java interfaces to represent the operations that you plan to use, along with separately-packaged implementation classes that wrap the Python code.
IN my opinion, Jython is exactly what you are looking at.
It is an implementation of Python within the JVM; as such, you can freely exchange objects and, for instance, inherit from a Java class (with some limitations).
Note that, its major strength point (being on top of of JVM) is also its major drawback, because it cannot use all (C)Python extension written in C (or in any other compiled language); this may have an impact on what you are willing to do with your text processing.
For more information about what is Jython, its potential and its limitations, I suggest you reading the Jython FAQ.
Simply run the Python interpreter as a subprocess from within Java.
Write your Python functionality as a proper script, which reads from stdin and writes to stdout.
Use the Java Runtime class to spawn a subprocess that runs your Python script. This is very simple to do and provides a very clean interface.
Edit
import simplejson
import sys
for request in sys.stdin.readlines():
args = simplejson.loads( request )
result = myFunction( args['this'], args['that'] )
sys.stdout.writeline( simplejson.dumps( result ) + "\n" )
The interface is simple, structured and very low overhead.
Remember to first check from those paying for the development that they're OK with the codebase needing a developer who knows both Python and Java from now on, and other cost and maintainability effects you've undoubtedly already accounted for.
See: http://www.acm.org/about/se-code 1.06, 2.03, 2.09, 4.03, 4.05, 6.07

Categories

Resources