Posix for Java.

Posix for Java

by Stuart D. Gathman
Last updated Jan 17, 2002

Why Posix for Java?

A frequently asked question on Java newsgroups is "How do I use unix signals (or messages or environment variables or ...) in Java?" The answer is, you can't with standard Java. Equivalent features are in Java. For example, Properties instead of environment variables or Sockets instead of the various forms of unix IPC.

However, sometimes it is necessary to interface with code written for the unix environment - and that code can't be rewritten to use Sockets. In those cases, it would be nice to have simple access to the Posix API without everyone having to reinvent the wheel.

The solution is the posix package. This package provides access to the posix API from Java. It uses a JNI library which should be portable to other posix systems. I started this package with the intent of making it reusable by others. However, it only has the classes I have needed for my own projects at present :-). I am making the source and docs public so that others can reuse what I have so far and so that I can collect any additions added by others.

The Passwd class

The Posix Signal API for Java

I will be adding features myself as time permits. For instance, unix signals seem to be a very common request. It is interesting enough that I have designed the class hierarchy. A single Java Thread listens for signals by executing semop() through JNI to wait on a semaphore. The native code signal handlers simply set a flag, post the semaphore and return. The Java Thread checks the flags and generates a Java 1.1 style SignalEvent for each signal.

A problem

The Signal code works, but the semphore id is not removed if the JVM terminates abnormally. Furthermore, semop() is not guarranteed async signal safe by posix. The posix call to use is sem_post, but that is not available in AIX. AIX does have some "np" thread calls that may fill the bill. It looks like signal handling will be #ifdef city. If you are only using Signal to do clean-up before shutting down the JVM, the current implementation should be OK.

Further Information

The JNI source is in C++. The C++ is used as a "better C". In particular, there are no static initializers, RTTI, or exceptions used since there is no standard JVM support for C++. (Only these C++ features require special runtime support.) We currently use GNUC 2.7.2 on AIX with the IBM JDK 1.1.6.4.

Here is the current source as a directory for your inspection. Send comments to Stuart D. Gathman.

  • The Javadocs.
  • Browse source online.
  • posix-1.1.1.tar.gz with Solaris 2.6 port.
  • posix-1.1.tar.gz.
  • Original 1.0 source.