Instead of using Jython, is there any way to call a Java program from Python?
This Java program contains some function and I need to give an input file to Java code and it returns two values which I will be using in Python. So the Python program passes a filename to the Java code and the Java code will return two values for each line in that file.
For example I have the following file containg data like given below:
22 16050408 2 2184 T:0.938645 C:0.0613553
22 16050612 2 2184 C:0.915751 G:0.0842491
22 16050678 2 2184 C:0.94826 T:0.0517399
22 16050984 2 2184 C:0.997711 G:0.00228938
22 16051107 2 2184 C:0.94185 A:0.0581502
I need give a file to the Java program and it will return two values for each line of that file. So the number of lines in the file containg the above data, input to java code will be the same. I need to replace the column two with two columns.. i.e. the values returned by Java.
Please kindly help
You can always call the java program as a command-line tool - it then does not really matter in what language the program is. For that purpose I would recommend using the subprocess module. You can even pipe the input/output to your python program without using any temporary files on the filesystem. Following example runs a java app and gets its output:
prog = subprocess.Popen(["/usr/bin/java", "TestClass"], stdout=subprocess.PIPE)
print prog.stdout.read()
Another option, although more involved would be using py4j:
Py4J enables Python programs running in a Python interpreter to dynamically access Java objects in a Java Virtual Machine. Methods are called as if the Java objects resided in the Python interpreter and Java collections can be accessed through standard Python collection methods. Py4J also enables Java programs to call back Python objects. Py4J is distributed under the BSD license.
I think this does sound like something you are doing..
The advantage of the Py4J is that it is a more portable solution using sockets to communicate between the java and python program - you are not running JVM inside your python environment, you literally "communicate" with existing JVM instance running the py4j gateway.
You can try JPype.
Here's a quote on usage from the documentation:
from jpype import *
startJVM("d:/tools/j2sdk/jre/bin/client/jvm.dll", "-ea")
java.lang.System.out.println("hello world")
shutdownJVM()
This will fire up a dedicated JVM where you can run your Java code, calling it transparently from Python.
Although in your case I believe you really could just run your routine as a subprocess, also having the advantage of easy switching from Java to anything else if you would need to do so in future.
Related
I'm using Python to launch a java program and to extract its results using os.system
os.system("java myprogram")
The problem is this launch the program on console and I have to type command inside the program to extract the result.
Do somebody know how to give command to the java program from Python ?
Thank you,
Easy way to invoke java program or any other script is using subprocess in python.
The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. This module intends to replace several older modules and functions
Read more about subprocess
Example:
subprocess.Popen - Following will invoke your script in background.
subprocess.Popen(["java", "ProgramPath/filename.java", "arg1", "arg2", "arg3"])
subprocess.call - And this will wait for the command to complete.
subprocess.call(["java", "ProgramPath/filename.java", "arg1", "arg2", "arg3"])
subprocess.check_output - will return you the output
output = subprocess.check_output(["java", "ProgramPath/filename.java", "arg1"])
http://www.jython.org/jythonbook/en/1.0/JythonAndJavaIntegration.html
This chapter will focus on integrating Java and Python, but it will explore several different angles on the topic. You will learn several techniques to make use Jython code within your Java applications. Perhaps you’d like to simplify your code a bit; this chapter will show you how to write certain portions of your code in Jython and others in Java so that you can make code as simple as possible.
So, I'm writing a program(mostly for practice, kind of for future convenience) that will show a java front-end when run, and depending on the input, will use a python script to crawl and interpret a webpage, then send the interpreted data back to java to format for the front end. I'm sure I can get this running on my own computer, but when I want to distribute my program for my friends to use, how can I ensure my java program, mainProgram.jar can find my python code, script_x.py. Is there a way I can assure my python script will be supported. I've read that there are ways to include the necessary runtimes and such with your program when you build it, but would it still interact the same way with my java program if it was bundled with it's dependencies? I can provide some sample code soon, but it's still a work in progress, as I don't have the python script finished yet.
here is a link https://bytes.com/topic/python/insights/949995-three-ways-run-python-programs-java
use Jython (compiles to java byte code) https://en.wikipedia.org/wiki/Jython
distributing Jython applications: https://wiki.python.org/jython/JythonFaq/DistributingJythonScripts
I want to write a Java program so that I can call it using the command-line, something like
java hearts/NewDeck | java hearts/Shuffle
which involves passing the output of the first command as the input of the second command. How should I structure the program? Should NewDeck/Shuffle be classes or methods?
Each of them is a Java program like all the others, it is not the JVM who controls from where the input comes but the OS that does.
Just both of them as normal Java programs (main method), no special needs here.
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).
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/