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/
Related
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.
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).
I am in the process of designing a MATLAB-based algorithm. Is it possible to call that MATLAB source code from a Java application?
Has anyone come across such a issue?
have a look at MATLAB Builder JA. This program allows you to convert your matlab code into java classes.
It is possible to call MATLAB from Java using matlabcontrol. It's a Java API designed specifically for that. However, it requires MATLAB to be running on the same machine that the Java code is executing on. As you put the Android tag I assume you want to run this on an Android device (although not mentioned in your description), and there is no known way to directly run MATLAB code on Android. matlabcontrol definitely will not do that, but you could use it to write a Java application which acts as a server for your Android application. Similarly, you could do the same with MATLAB Builder JA which generates code that needs either MATLAB or the MATLAB Compiler Runtime (which is available for free - so users would not need to buy MATLAB).
Is there a java api similar to RAPI? I want to be able to access files on the windows mobile device using a java desktop program.
Thanks.
You could use RAPI itself, and access it from Java using JNI or a wrapper like Swig
We were also looking for a similar API in Java but unfortunately none is available. I wrote my own RAPI wrapper using JNI and used that in my program.
The main problem with JNI is that any un-handled exceptions/faults cause the calling Java program to shutdown as well. Do keep that in mind when writing your wrapper. There are different approaches to safe guard these, the simplest and common approach is to write a standalone program written in .Net/C++ that communicates with the RAPI and your Java program communicates with that program using pipes/files etc. this way you also don't have to write a JNI wrapper :-)
I have written python scripts that use scrapy,nltk and simplejson in my project but i need to run them from java as my mentor wants to deploy them on a server and i have very less time to do this.I took a glance at runtime.exec() in java and jython, needless to say that running system commands from java doesn't look simple either.
So I would like to know if running the python scripts from java as system command -'python example.py ' using runtime.exec() or alternatively using jython would be more simpler and actually feasible or whether there is a simpler workaround .It would also be great to know if anyone had run python code that uses nltk from java using Jython and whether they encountered any problems.Please help me as I have to do this as asap.Any thoughts and suggestions regarding this are welcome.
Thanking in advance!!
The Jepp project lets you call python scripts from Java. It provides an easy mechanism to pass variables into a script and extract values back. I've used on a few projects with good success