Given a Java function generate a command-line wrapper [script] - java

I'm searching for something like a Java version of https://github.com/google/python-fire
I want to write a Java function that, given a reference to another Java function will give me some command-line program (e.g. a shell script) which can be launched with different arguments which are then passed to the given Java function as arguments.
How hard is this? How can I do this?
An example of argparse-based generated command-line wrapper for python: https://github.com/kubeflow/pipelines/blob/ee527c8ad4c6fd60e8ee8a37f0653330ed0dc426/components/gcp/automl/create_dataset_for_tables/component.yaml#L80

Related

Use Java program from Python

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.

Calling Java class/method using command line

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.

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.

How do I check what version of java im using in c++

I have a testing script that checks what version of java the user is using and then executes some commands. But we are trying to convert all of our testing into cxxtests. I would like to know how to convert my version checking script into c++ code. I know I could just use the system() command but I would like that to be a last resort. Any help or leads would be appreciated.
Thanks,
Josh
You have to use the system() command, or a C++ library that will use the system() command. This task is much better done in a shell script than C++ code. If necessary, the shell script can call C++ code to do notifications, or whatever it is that's causing the reliance on C++.

Categories

Resources