Is there any Java tool to communicate with an external CLI? - java

I need a way to communicate with an external tool which has a Command Line Interface from my Java application. Is there any handy tool/lib to make it less painful? Believe me, I've googled enough. Thank you!

You can use Runtime.exec() for executing external commands of the operating system. Be aware, though, of the gotchas of using it - make sure to read this article first.
If that's too simplistic for your needs, take a look at ProcessBuilder. It's available since Java 1.5, from the release notes:
The new ProcessBuilder class provides a more convenient way to invoke subprocesses than does Runtime.exec. In particular, ProcessBuilder makes it easy to start a subprocess with a modified process environment (that is, one based on the parent's process environment, but with a few changes).

I've ended up using the process handler (more specifically, the com.intellij.execution.process.OSProcessHandler) from Intellij IDEA platform API + writing my own small command-based framework where each command knowns how to execute itself (write) and parse the response from a process (read).

Related

How to create a GUI in Java that runs another language in the background?

So for a hackathon I want to use this domain-specific language, Cryptol, in order to create an encryption system. However, I want a GUI for said system.
I have no idea how I could write a GUI in Java that run Cryptol in the background. Is this possible?
Thanks
This is a very broad question, however, let me take a stabe.
There are multiple ways to create a GUI in Java (JavaFX, JSF, JSP, Swing to name a few). Without any experience, I would say JavaFX is the easiest to get started with. But that is completely opinion based of course.
When there is no standard API (library) to interact with Cryptol, you should create a rudimentary API for yourself. How? That completely depends on which functionalities from Cryptol you need and how Cryptol exposes it's interfaces.
You can use the ProcessBuilder class in java to execute programs through the command line. Essentially, ProcessBuilder allows you to execute programs as if you were to execute them in the command line by passing a string to the ProcessBuilder constructor. You can then read the response from ProcessBuilder to view the output of your other programs. Here is the documentation for Process Builder -> http://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html

Java out of process component for multithreading

I recently started to work in a java project and i am from vb background.
Is there any equivalent of VB6 ActiveX Exe development in java which enables a component to run in its own process and useful in multi threading requirements?
I think the only way is to start another JVM from the JVM you're currently in.
But I don't really see why someone would want to do that in a real-world scenario.
Apart from that all multi-threaded execution happens in the same JVM you're in.
Java has API for working with external processes.
https://docs.oracle.com/javase/7/docs/api/java/lang/Process.html
You can use ProcessBuilder to create it.

How to use multiprocessing with Java - NOT multithreading

I've been looking for an api for simple java-based multiprocessing and couldn't find anything.
I have legacy code which I need to integrate into my java application. Since this legacy (native) code crashes sometimes, the whole jvm crashes with it. So what I want to do, is to run this code and its adapter in a different process (not thread!).
There's the ProcessBuilder in the newer java jdks, it lets you start a new process and gives you a In/Outputstream; so solving my problem is possible by hand. In order to do so, you have to find the path to your jvm and start it up with the for your process. Then you have to use the stream to communicate.
Is there something which takes over that job? Or do I really have to do it by hand?
AFAIK, people usually do this by hand.
The problem is that there's no truly portable way of doing it ... what with the difficulty (impossibility) of identifying where the currently running JVM was launched from, and the fact that JVM command-line options are vendor, version and (potentially) platform specific.
The simple solution is just to put the JVM path and options for launching the child JVM into some configuration file.
You can use the -Dprocess.name=$1 and let your main class take in some command line args. You can invoke by calling something like this:
java -cp $CLASSPATH $VM_ARGS $MAIN_CLASS
and your VM_ARGS can be defined something as
VM_ARGS=" -Dprocess.name=$1"
FWIW, I wrote a replacement class to take care of a lot of the I/O stream redirection nastiness, at david.tribble.com/src/java/tribble/util/RuntimeExec.java
You are looking for the technology called Java RMI (Remolt Method Invocation).
This allows one JVM to call a method in another JVM. This can be on the same machine or over a network.

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/

How to know start and kill processes within Java code (or C or Python) on *nix

I need to write a process controller module on Linux that handles tasks, which are each made up of multiple executables. The input to the controller is an XML file that contains the path to each executable and list of command line parameters to be passed to each. I need to implement the following functionality:
Start each executable as an independent process
Be able to kill any of the child processes created, independent of the others
In order to do (2), I think I need to capture the pid when I create a process, to issue a system kill command. I tried to get access to pid in Java using ProcessBuilder but saw no easy way to do it.
All my other logic (putting info about the tasks in DB, etc) is done in Java so I'd like to stick with that, but if there are solutions you can suggest in C, C++, or Python I'd appreciate those, too.
For a Java solution, you should take a look at the apache commons exec library. They've done a lot of work to make it platform independant and they have a great tutorial.
In python, you can use the included subprocess library.
You really really need to look up "shell scripting" on google. Specially if your employer/instructor wants you to work on linux and deal with processes etc.
Maybe start here:
http://supportweb.cs.bham.ac.uk/documentation/tutorials/docsystem/build/tutorials/unixscripting/unixscripting.html
I'm not sure, but if you start executables from Java, you may start them in seperate threads, and then you can map them however you want - by name, by line number or something - and stop that enclosing thread regularly as java-thread, which doesn't seem to be an elegant solution (not closing files, etc.), but could work to some extend (as long as the linux-program doesn't start a process, which is freeing itself from its parent).
Specific commands for closing each process, send via stdin to the programs, might be another option. How to handle stdin and stdout and other pitfalls are mentioned here in some length:
http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?
Visible programs can even be controlled by java.awt.Robot (keyboard, mouse).
As a last idea I would consider using a new command "kill pidof program" which working on a name-basis, so you can't distinguish two instances of the same progam.
I don't know the apache-lib, mentioned by Steen, but there is normally very useful stuff, I would recommend to look there too - maybe in the first place.

Categories

Resources