Too Many Open Files Errors When Using Runtime.exec() or ProcessBuilder.start() to Execute A Process

Java enables you to exectute a process on the host OS and to provide input (STDIN) to the process and read standard out (STDOUT) and standard error (STDERR).  Each time that you execute a process via Runtime.exec() or ProcessBuilder.start() STDIN, STOUT, and STDERR are piped to the running JVM as an OutputStream or InputStream.  For each stream there are two file descriptors and typically when the process terminates those streams are closed also closing the two file descriptors.

Even if you destroy the Process via Process.destroy() you may still be leaking file descriptors.  The typical symptoms are as follows:

1. You see a growing number of named pipes in the output of lsof:

java      8654 rchapin   23w     FIFO        0,6        0t0      26015 pipe
java      8654 rchapin   24r     FIFO        0,6        0t0      26016 pipe
java      8654 rchapin   26r     FIFO        0,6        0t0      26017 pipe
java      8654 rchapin   30w     FIFO        0,6        0t0      26031 pipe
java      8654 rchapin   31r     FIFO        0,6        0t0      26032 pipe
java      8654 rchapin   33r     FIFO        0,6        0t0      26033 pipe
java      8678 rchapin    0r     FIFO        0,6        0t0      26015 pipe
java      8678 rchapin    1w     FIFO        0,6        0t0      26016 pipe
java      8678 rchapin    2w     FIFO        0,6        0t0      26017 pipe
java      8680 rchapin    0r     FIFO        0,6        0t0      26031 pipe
java      8680 rchapin    1w     FIFO        0,6        0t0      26032 pipe
java      8680 rchapin    2w     FIFO        0,6        0t0      26033 pipe

2. You start seeing the ‘Too many open files’ error in your log:

2012-07-06 02:39:27,260 (ERROR) (Thread-6) java.io.IOException: error=24, Too many open files

A good practice is to make sure to get a reference to each of the Streams and to explicitly close them, REGARDLESS of whether you actually utilize those streams.

Example:

Process process = Runtime.getRuntime.exec(“some command here”);
or
ProcessBuilder processBuilder = new ProcessBuilder(someCommandArray);
Process process = processBuilder.start();

// To close them
process.getInputStream().close();
process.getOutputStream().close();
process.getErrorStream().close();

Creating an Eclipse Java Project from an Existing Directory Structure and Source

If you are keeping your code and configs in a repository, more likely than not (and it is recommended) that you do not also store your IDE specific configurations or files within the repo.

As such, you will only keep the bare minimum of IDE independent files in your repository and when you check out a copy of it you will have an existing set of directories and files that you will use to create a project with in Eclipse.

To create a new project in Eclipse using the existing directory structure, do the following:

  • In Eclipse go to: File/New/Project
  • Select “Java Project”
  • Un-check “Use default location” and then click Browse and navigate to the top level directory with contains your source, libs, configs, etc.
  • Eclipse will display a warning that says that your project “overlaps the location of another project”.
  • Making sure that the “Location” field now contains the path to your source directory, RE-check the “Use default location” checkbox and now Eclipse will let you click on the “Next” button to continue configuration of your project.

Problem Adding a Network Printer Under Windows 7 on a Samba Share

Ah, Windows.  It still has it’s old, quirky, you-just-can’t-wait-for-MicroSoft-to-go-out-of-business charm.

But seriously, if you are having trouble adding a printer on a Samba share on a Linux host try the following:

  • After you authenticate to the Samba share, make sure that you actually open up a window displaying the contents of the home directory.  For some reason, just authenticating to the Samba share will not get you access to the Samba servers list of shared printers.
  • If the searching for printers window hangs without displaying the Samba share’s printers, hit the cancel button, close the window and try it again.

Syntax for Dynamically Generating JSON in JavaScript to be Output to HTML

Should you find yourself in a situation where you want to call a function (or method) that accepts JSON data parameters that you want to actually print to the browser to be invoked on some sort of callback (onchange, onclick, etc).

Given the following function:

doSomething(JSONparam) {
    // Something happens in here . . .
}

Here is the syntax for rendering an onclick link that is generated in JavaScript and then output to the DOM:

var $markup = ‘<a href=”some/url.html” onclick=”doSomething({\’key1\’:\’value’\, \’key2\’:\’value\’}); return false;”>Some link</a>’;

Simple Solution for JavaScript Object Packages and Namespaces

Most OO programers are familiar with packages.  This enables programmers to group like classes together in a logical order and provide some namespace to avoid class name collisions.

I have seen a variety of JavaScript sugar implementations that are mostly unintelligible.

I like things that are simple and easy to understand.

So, if you have a JavaScript class that looks like:

/**
 * Constructor
 */
com.ryanchapin.Base = function() {

    //
    // Constructor does something here . . .
    //
}

//—————————————————————————-
// Properties:
//

/**
 * Some property
 */
com.ryanchapin.Base.prototype.someProperty;

//—————————————————————————-
// Methods:
//

com.ryanchapin.Base.prototype.doSomething = function() {

    //
    // Do something here . . . .
    //
}

Then all you need to do is add something similare to the following as the top-most block of JavaScript on the page:

<script type=”text/javascript”>

            // Instantiate ‘package/namespace’, containing object
            var com = new Object();
            com.ryanchapin = new Object();

            // Then you can instantiate your objects as follows:
            var baseInst = new com.ryanchapin.Base();

</script>

This will create the ‘container’ object in which your class definitions will reside and help reduce the use of global variables as well as class name and method name collisions.

Solution for ‘NX service is not available’

When first installing NXServer I have seen a number of occastions when the initial run of

# /usr/NX/bin/nxserver –install

Does not necessarily work correctly and simply running the following commands has worked.

# /usr/NX/bin/nxsetup –uninstall –purge –clean

then

# /usr/NX/bin/nxsetup –install –setup-nomachine-key

If the previous uninstall/install command doesn’t work, try rpm -e nx*.rpms and reinstalling the rpms before trying to run the nxserver –install command again.

Resources for Understanding Object-Oriented JavaScript

Following are some good articles/tutorial on OO JavaScript: