Using a ProcessBuilder to Execute an OS Level Command and Properly Read the Exit Code from a Java Process

There are many a situation where a developer will want to execute an OS level command in another process and read not only the standard out (stdout), and standard error (sterr), but also the exit code returned from the process.

To do so, utilize the ProcessBuilder class along with a helper class (ProcessWrapper) that will provide the ability to set a timeout for the process and read the exit code in a separate thread.

Following is an example and the → Continue reading “Using a ProcessBuilder to Execute an OS Level Command and Properly Read the Exit Code from a Java Process”

Dynamically Instantiating Classes in Java

Edited 2021-02-08 to reflect API changes for Java 11.

There are a number of cases when you may not know exactly what class you will be instantiating and want to be able to dynamically instantiate specific classes based on a configuration file or a condition during runtime.

To do so, you will need to define a parent class, abstract class, or interface for your classes and can then use the following code as a guide.

Given the following class definitions:→ Continue reading “Dynamically Instantiating Classes in Java”

Preserving File Permissions When Copying Files with Ant

If you want to retain the file permissions of a file that you copy with ant you must use the <exec> command instead of? <copy>.

So, instead of:

<copy file=”${source.file}” tofile=”${dest.path}/${dest.file}” overwrite=”true”/>

Use:

<exec executable=”cp”>
? <arg value=”-pf”/>
? <arg value=”${source.file}”/>
? <arg value=”${dest.path}/${dest.file}”/>
</exec>
Continue reading “Preserving File Permissions When Copying Files with Ant”

java.lang.OutOfMemoryError: unable to create new native thread Exception When Using SmbFileInputStream

I am writing an application that copies files from a Samba share using the JCIFC Java CIFS Client Library (which, btw, is a very helpful, widely used, and well developed project).

As usual I am under the gun to get something up and running so I do not have the luxury to explore all of the details as much as I would like. My application gets a directory listing of a Samba share and then spawns a new thread for → Continue reading “java.lang.OutOfMemoryError: unable to create new native thread Exception When Using SmbFileInputStream”

Adding a ShutdownHook to a Java Program to Enable Clean Exit on CTRL-C

Often times I will be building a Java program that will either be run from a command-line or as a system service.  In most cases, during development, I’ll be running it directly from the command-line and will want to kill it by pressing control c on the keyboard.

When I do that I want to make sure that my program cleanly exits, closing any open files, socket connections, database connects, or what have you.

To do so, you must invoke → Continue reading “Adding a ShutdownHook to a Java Program to Enable Clean Exit on CTRL-C”

Replacing Smart Quotes (<91> and <92>) in vi

Smart quotes will be displayed as <91> and <92> in vi. A number of other special characters will also be displayed in similar fashion <96>, etc. . . .

To replaced them use do the following:

  1. Type:? ‘:’ 1,%s/
  2. Then, since it is a special character, you can’t simply type ‘<91>’ or that string will not be matched, so:
  1. Press: CTRL and V
  2. Then type: [space] x [space] 91
  3. [Enter]
Continue reading “Replacing Smart Quotes (<91> and <92>) in vi”

Configuring CentOS to run SELinux in Strict Mode

I am in the process of setting up some CentOS/RHEL 6 servers to run SELinux in strict mode. What follows are notes, links to online resources and things that I am discovering along the way. Once I am finished I will go back and re-write it to follow more of a how-to/guide type format. In the meantime, it might seem a bit disjointed.

Links/Resources:

  • http://wiki.centos.org/HowTos/SELinux
  • http://fedoraproject.org/wiki/SELinux
  • http://www.centos.org/docs/5/html/Deployment_Guide-en-US/rhlcommon-chapter-0001.html
  • http://www.nsa.gov/research/selinux/index.shtml

MaintLog Notes:

  • Make sure that the selinux-policy-strict package (and deps) are installed:
Continue reading “Configuring CentOS to run SELinux in Strict Mode”