Can Python invoke the Java Framework? - java

Can Python invoke the Java Framework?
I want to know whether a Python project can invoke a Java Framework, I find a Java Framework in GitHub, whether I can use it in my Python project?

Jython
Jython is one method of calling Java from python -- actually, you run your Python inside Java JVM. This gives you access to almost any Java that runs on JVM, but comes with many limitations.
Because Jython is running python inside the JVM, this gives you acess to almost any Java library. However, you are very restricted in what Python you can use: you can only use Python 2.7, and can import pure Python libraries only (compiled Python libraries with C will not run on Jython).
For an example of a project that uses Jython: Processing.py runs on Jython in order to access the Processing Java API and its ecosystem of Java libraries.
https://github.com/jdf/processing.py
Note that Jython 2 and its docs are quite old, and that the developers are uncertain if / when Jython 3 will be released.
https://github.com/jython/jython3
py4j
py4j is a different approach -- it is "A Bridge between Python and Java" and lets native python code access separate Java running in a separate JVM. Note however that the python and Java code must be running in parallel and communicating through a gateway interface. This is communication between separately running processes -- you are not spinning up a JVM from Python or inside Python.
For example: on the JVM side pass myObject to a new GatewayServer(myObject); on the Python side create a JavaGateway() Python object and use it to communicate with the Java myObject.

Normally Python and Java have their own interpreters/VM's and cannot be shared. It is possible to use Jython but has limitations (fe. python version and support/compatibility with other python packages).
The interpreter and JVM do not match: Java has strict typing, python not. Java compiles and run, python is an interpreter and can change code in runtime (if you want). These are extra challenges why putting all in a same environment is very complex.
There are possibilities like a client/server architecture, but the feasability depends on the level of the framework.
Most of the time low level frameworks are optimized to run directly inside your application process. Any loose coupling will introduce performance and security and compatibility issues. Just think about how reflection will work or multiple inheritance.
If it is a high level framework (fe able to run stand alone) it is more feasable to use some sort of client/server. But still you have to develop a lot for it.
Industry standard is just to implement the framework of your desire in the language you want, then you can get also all the benefits of your platform.

Related

Java Projects on Django?

ok, So i searched net for the possible implementation but all that I managed to find is Django projects implementation on Java platform through Jython. But I want to do the reverse, i.e. implement/integrate java project ( which in my case is SAIKU server ) on Django platform.
The question being, is it possible, and if yes, then kindly point me to the solution.
Thanking in advance =)
For your specific requirement, I would suggest using RESTFul API to access the Saiku Server.
However if you need to run Java Classes from Python.
Here are the options available for you:
JCC -- a C++ code generator for calling Java from C++/Python. It produces producing Python extensions which communicate via JNI with a Java virtual machine. As it implies, this would require compilations of every possible call. However this project is backbone of PyLucene project.
CodeMesh. C++ code generator for Java.
Py4J Python programs running in a Python interpreter to dynamically access Java objects in a Java Virtual Machine.
JPype allow python programs full access to java class libraries. It is done through interfacing at the native level in both Virtual Machines. However there are no recent development in this front.
In general, having an loosly coupled integration through REST or RCP would be easy to maintain than tightly coupled JNI based implementation.
There's no way to run Java within the Python runtime (which is what it sounds like you want). There are Java to Python "translators" available, but they're terrible. Honestly, if you need a Java server and Django to sit inside the same process for some reason, Jython is the way to go.
There are lots of options outside of that though, off the top of my head:
Implement Python bindings for your server (See PyLucene for an example)
Implement a socket server within your Java server that Python can talk to directly

Calling C++ executable from JAVA

I have poker bot written in C++. I want to compete it with another Poker Academy agent. In order to do that there is an API in JAVA namely "meerkat api".
I have no idea how to call my c++ executable from java api, how does one go about communicating with the independent c++ executable from within a java package?
To get started see:
java.lang.Runtime.exec() family
java.lang.ProcessBuilder
Or if you need to integrate with the C++ app on lower lvl meaning not only "run a program and process the results". You can use the JNI or easier variant JNA, but I have to admit it is pain.
After reading/following #hmjd, Well you can use DefaultExecutor from Apache Commons Exec library to execute commands but it internally uses java.lang.Runtime and java.lang.Process.
I would suggest you to use this library over Runtime because Apache Command Execution APIs are more sophisticated, and provide all and more features than Java Runtime. It also handles exit Values.

Use python library in java code

There is some library called pymorphy written in python. Unfortunately, for java there is not any library with the similar functionality - natural language processing for Russian lang. So I need to invoke some methods of pymorphy library from Java code.
First I've tried to solve this problem with Jython. But I've spent 2 days and the goal was not accomplished because python modules cdb, bsddb3, sqlite are written in C and they will not work with Jython.
Now I want to run some python light-weight server with pymorphy for handling request from Java code.
How could I implement this kind of java-python interaction with the maximum production performance? Or is there more simple way to call python from java?
Try Jepp, "Java Embedded Python". http://jepp.sourceforge.net/
I haven't used it beyond small projects, but it works as advertised, allowing one to call CPython transparently from Java. If you have the opposite problem, needing to call Java from CPython, definitely check out JPype. I've used it extensively and it works very well.
I think these libraries (cdb, bsddb3, sqlite) has a jython implementation in https://code.google.com/p/django-jython/ check it out

Run m-files Inside Java

I have a matlab function written in an m-file. (it's not written by me and seems a complex algorithm) Now I want to use it with a java application. I searched in internet and learned how to run matlab codes inside java.
For example here it says how to do that. But I can't understand how to use the exact matlab function as it is in java without knowing what function does. Can someone help me to figure this out?
I am not sure that MatlabControl is what you need. It merely runs Matlab as server and sends it commands. You will not be able to give it to your users, unless they are all willing to pay for a Matlab installation.
If you want to deploy your application, consider using Matlab Builder JA.
From the website:
MATLAB Builder™ JA enables you to create Java™ classes from your MATLAB® programs. These Java classes can be integrated into Java programs and deployed royalty-free to desktop computers or Web servers that do not have MATLAB installed.
Also, if you use MatlabControl, there might be a compatibility problem in the future - check this out:
This API relies upon the Java MATLAB Interface distributed with all recent copies of MATLAB. This interface is entirely undocumented and there is no guarantee matlabcontrol will work with all versions of MATLAB and operating systems. Efforts are made to ensure compatibility with MATLAB R2007b and greater, see here for specifics.

Can python modules be used from Java programs using Jython without modification?

Can Python code be used from Java using Jython, without modifying the Python code in a way which will prevent it from working correctly in CPython?
If yes, what steps would have to be taken (in the Java code)?
If not, what are the reasons that this cannot be done (so far)?
Python modules can depend on certain Python versions (e.g. Python 3 vs Python 2 and even may require a minimum Python version (e.g. 2.6) in case of using dedicated language features introduced in some Python version)
Python modules may depend on C extensions which won't work with Jython
Python modules may use CPython features that are not available in Jython
In general: most Python-only code should work with Jython - however like in all cases: you have to test, test, test. Good written modules provide unittests - so you should try to run the tests from Jython and see what's happening.

Categories