Calling C++ executable from JAVA - 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.

Related

Can Python invoke the Java Framework?

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.

How to create java interface for existing ruby project

my project has a significant and isolated part that was written in ruby(jruby compatible). It is a commandline application that we run it in the terminal and provide it with various option flags.
My client wants to use this tool but only willing to use it if it is wrapped in a java class. I went through a lot of trouble to convert the ruby code to java by using jrubyc --javac A.rb. Inspecting the converted .java file, it is calling a Ruby Runtime to execute the ruby script. Like this:
org.jruby.Ruby.getGlobalRuntime().executeScript(stringBuiltFromARubyFile, 'path')
My question is performance wise, is this better than just wrap the create a runnable jar, wrap it with a java class that takes certain parameters, and execute the jar via Runtime.getRuntime().exec("java -jar A.jar args") ?
The application A.rb uses multi-threading. I bring in ruby dependencies (gems) using jruby-gradle plugin.
What are some other options I can explore?
Thanks in advance.
The approach you are using likely won't work. You are launching a JVM within a ruby context; and what you've been asked for is launching some of your ruby code within a JVM context.
I would look at C to Ruby bindings, and then use a Java to C (JNI) interface to launch the required Ruby code from the C layer. If such a thing is not practical, as your Ruby is more of a standing service than a CLI process, I would then consider making a set of Java libraries to call the process through a network call.

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

Executing a Matlab function using java

I'm writing an application which does image processing using matlab and later displays the result using Java's Interface. Due to certain reasons I've to use both Java and Matlab.
How can I use the matlab function in java ?? How to create and access the interface.
matlabcontrol is a Java API which will allow you to call your image processing functions or scripts and return the results to Java. To get started, take a look at the walkthrough.
MATLAB Builder JA is one option to use MATLAB code in Java. This is a non free toolbox for creating a jar file from MATLAB code in order to be imported to Java. Take care of the restrictions concerning jar file creation.
Using Java classes inside MATLAB is much easier, as you can instantiate Java classes in MATLAB code. Undocumented Matlab is one valuable resource for Java integration in MATLAB.
Check Using Sun Java Classes in MATLAB Software for the official information provided by The MathWorks.
A quick google brought up this http://j-integra.intrinsyc.com/support/com/doc/other_examples/Matlab.htm
Alternatively, can you execute your matlab function from the command line? If so you can use
Runtime.getRuntime().exec("your matlab function")
As of R2016b you can use official MATLAB Engine API for Java, which seems to allow same functionality as matlabcontrol.

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