Use Java program from Python - java

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.

Related

How to package a python script with my java program?

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

Execute java class from php

I want to execute some java class for doing some bacground work,(have no any ui related task).like file read, write in a specific location. My main server side is done by php. I want to call these java program from php file. I study about Java/PHP Bridge. But i could not understand what i have to use like jse program(not suitable in server),jsp application (have no gui), or by a ejb. Please tell me what will be the best option to do that?
You can execute a program in a couple of ways:
exec()
system()
passthru()
shell_exec() or the shell execution operator (the backticks, ``)
Which one to use depends on what you want. You should study the above links and figure out which one to use.
Use the exec() function:
exec("java Main.class");

How to write a single script enabled java testing application?

I'm trying to finish a task which requires me to write a standalone java application that can be invoked by a single script and run automatically. Java JDK location and other arguments like output path are all needed to be involved in the command line, 'cause as well they also need to be tested. I've no experience of writing this kind of script, can anybody give me some hints? Many thanks.
Write some sort of shell script which calls your Java program with the necessary arguments. It could be as simple as one or two lines.
You may find this easier if you package your Java program as a JAR which includes all of its required dependencies.

Python with java

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.

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/

Categories

Resources