Monthly Archives: February 2012

Calling C From Java Is Easy

Sometimes we need to access operating system functions that the standard Java API doesn’t expose, or use non-Java libraries. Although it’s well known that you can call this “native code” from Java using JNI, there is not so much entry-level material on how it’s actually done. It is often left out of introductory material–including the official [...]

Runtime.exec with Unix console programs

Ever wanted to launch less or vi from a console Java program to show or edit a file, only to find that they won’t work like when you launch them from the terminal? The problem is that these programs need to communicate with a TTY (teletypewriter) device to find the screen size and to be [...]

Software Design Maxims

max·im [mak-sim] noun (now rare) A self-evident axiom or premise; a pithy expression of a general principle or rule. A precept; a succinct statement or observation of a rule of conduct or moral teaching. These are my maxims when designing software. DONE is better. A design can never be perfect; you’ll always think of a way to make it better. A design should be just good enough to ship, rather than delay the release by months to [...]

The JVM ate my variable!

Consider this code: Object obj = new Object(); WeakReference<Object> ref = new WeakReference<Object>(obj); List<byte[]> filler = new LinkedList<byte[]>(); while (ref.get() != null) { filler.add(new byte[1000]); } System.out.println(“Filler size ” + filler.size()); When executed it should always run out of memory: ref.get() will not return null because the referenced object cannot be garbage collected. The local [...]