Calling Java class/method using command line - java

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.

Related

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

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

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.

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 to run Java code using Java code?

Basically, I want to do two things:
I want to know if there is any way that I can run Java code using Java code.
If it is possible, how would I show the output on my screen? (be it regular output or error or exception)
I know this is possible because one of my seniors had done it, but I don't know how he did it. Maybe he used one of Java's built-in classes.
Note: user will write the code in some text file and then I will store that file content in some variable and then maybe run that code.
Yes, it is possible.
Step 1: Compile the Code
Use ProcessBuilder or Runtime to construct a Process in which the Java compiler compiles their code. (Note that this requires that a Java compiler be available on the system at runtime).
Step 2: Invoke their Code
There are two ways to invoke their code. You can again use a ProcessBuilder or Runtime object to construct a process in which you execute their Java code. You can use the Process's getInputStream and getOutputStream functions to read from and write to the other process. An alternative is that you can use Class and the various reflection APIs to load their code and execute it directly within Java.
You would have to some how invoke a compiler (such as Suns javac), parse its output in case of errors and load the resulting classes dynamically.
There is no API-classes in the Java runtime library that will parse, compile and run Java source code.
If you want an interpreter (so, no compiling required) for Java, take a look at BeanShell.
I like this one very much!
You can use a scripting language running on top of the JVM. Groovy is a very good idea and it has very similar syntax compared to Java.

Categories

Resources