A Posix to Java Binding

Introduction

This is a binding of the Posix systems calls used by all major Unix systems to Java. This means you can write applications that exploit the features of the Unix Operating System. These applications are not ``Pure Java'' since they rely on non-standard native code libraries.

Running applications

The make install part of the build process will load the native code and C libraries into standard locations. By default these are

In order to run an application using these, appropriate environment variables must be set. For example, using bash

    CLASSPATH=/usr/local/classes/posix.jar:$CLASSPATH
    LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
    export CLASSPATH LD_LIBRARY_PATH

For compilation, only CLASSPATH needs to be set.

Every application must dynamically load the Posix library before any calls to Posix objects are made, by

    System.loadLibrary("java_posix");

Example

This Posix C program to copy standard input to standard output is
#include 

main()
{
    const int SIZE = 1024;
    char buf[SIZE];
    int nread;

    while ((nread = read(0, buf, SIZE)) > 0)
        write(1, buf, nread);
}

The Java equivalent is

import posix.Posix;
import posix.PosixException;

public class CopyIO {

    public static void main(String argv[]) {
        System.loadLibrary("java_posix");

        final int SIZE = 1024;
        byte buf[] = new byte[SIZE];
        int nread;
        try {
            while ((nread = Posix.stdin.read(buf)) != 0)
                Posix.stdout.write(buf, nread);
        } catch(PosixException e) {
            System.err.println("Error in copy " + e.toString());
        }
    }
}

The test directory contains further examples.


Jan Newmarch (http://jan.newmarch.name)
jan@newmarch.name
Last modified: Sat Jun 20 19:09:49 EST 1998
Copyright ©Jan Newmarch