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”/>


<exec executable=”cp”>
? <arg value=”-pf”/>
? <arg value=”${source.file}”/>
? <arg value=”${dest.path}/${dest.file}”/>
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”