zt-exec

 

zt-exec is a library for running external commands/programs from Java, which improves upon the functionality offered by Java's ProcessBuilder class. For example, it simplifies the handling of IO streams, supports asynchronous tasks, and makes it easier to use timeouts, exit codes, and logging. The website also includes a series of common use-cases, all coded in just a few lines.

zt-exec is utilized with seven tools on this page: a short program that calls Java, an example employing Pandoc, Windows' SystemInfo command, Windows' Powershell, Windows' netstat, Gnuplot, and GraphViz. Each tool is explained in a little more detail below, and all the examples can be download in a zip file from the "Downloads and Other Links" section.

1. Calling Java

BasicExec.java makes an external call to Java -version, the preferred example on the zt-exec web page. However, this version uses my ZUtils.java functions.

2. Calling Pandoc

Pandoc can convert a very wide range of file formats into other forms. However, the code in PandocTest.java focuses on converting Markdown to MS Word DOCX or Powerpoint PPTX.

Two Markdown files are used – md-syntax.md and conrad.md, and a picture of a bench.

For more information on Markdown, see: here, here, here, and here.

3. Calling SystemInfo

WinSys.java uses Windows' SystemInfo command to obtain system details. The data is stored in CSV consisting of two rows, one for the headers and one for the data. ZUtils.buildMap() converts this into a map of headers to data, with the help of Apache Commons CSV.

More details on SystemInfo can be found here and here.

4. Calling Powershell

WMITest.java illustrates some uses of Window's Powershell, concentrating on retrieving system information using the get-wmiobject cmdlet and various WMI classes.

More details on Powershell can be found here, here, here, and here.

More details on get-wmiobject can be found here, here, and here.

Since PowerShell 3.0, get-wmiobject has been deprecated in favor of the very similar get-ciminstance. And in Powershell 7 (the current version), WMI was dropped, and you have to use get-ciminstance. However, I'm still running Powershell 5.1 and Powershell 2 (!) on my test machines, so I've remained faithful to get-wmiobject.

5. Calling netstat

NetstatTest.java utilizes netstat -f on Windows to print my machine's 'Established' HTTP connections, updating the results every few seconds. The code relies on a call to zt-exec's ProcessExecutor which collects output line-by-line while the process is running.

More details on netstat can be found here, here, and here.

6. Calling Gnuplot

GnuplotTest.java employs Gnuplot to plot an assortment of 3D functions, some of which are shown below.

[Gnuplot Plots]

For more conventional 2D charts, the JFreeChart Java library is a good alternative.

7. Calling GraphViz [GraphViz graph]

GraphvizTest.java draw several graphs by calling GraphViz, one of which is shown on the right. The graph is displayed using my ImageViewer class.


 

Downloads and Other Links


Dr. Andrew Davison
E-mail: ad@coe.psu.ac.th
Back to the third-party libraries page