Category Archives: Java

Java and File Names With Invalid UTF-8

On Unix systems – Linux and OS X included – file names can be arbitrary binary data with very few limitations. This means that in order to make sense of the name a character encoding must be used. Recently UTF-8 has become the default encoding on many systems, but sometimes you have to deal with [...]

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 [...]

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 [...]