Puzzles

This is a series of Java puzzles that put your Java skills to the test, in a challenging and fun way!
A puzzle consists of some given Java code with a line in it that seems to be impossible to reach. It’s up to you to find the hole in it, abuse a subtle behavior of Java to make execution reach that line anyways.

Rules

Short version: You must run the program with java -Djava.security.manager the.MainClass. Any cheating in the content of your .java file is allowed; it is the whole point of the puzzle. Cheating the (compiler or runtime) environment from outside your code is not allowed.

If that's clear, then start puzzling now and forget about the rest. But if that still leaves you in doubt:

  1. The goal of each puzzle is to reach a certain line in the exact given class. Reaching the line in a modified copy of the given classes does not count.
  2. You can use any unmodified Java compiler and runtime. Puzzles are not intended to be version-specific so I recommend you just use something recent like JDK 6 or 7 or 8.
  3. No extra arguments to the compiler or runtime except for the -Djava.security.manager are allowed. The security manager option is there to prevent things such as setAccessible that would spoil the fun. If you like to work from within your IDE, add that option to the JVM arguments of the run configuration. If you don't change any other funky settings you're good to go.
  4. The solution consists of just the content of the one .java file. Other files may not be modified, and if any extra steps or tweaks are needed to be able to make it work, it's not a valid solution.
  5. Within that code everything is allowed, except exploiting known security holes in Java itself. You can write ugly things that would not be acceptable in any decent program. If you really feel like it and can pull it off, you can make a classloader subclass dance naked on a reflective error. But it's unlikely to help you get closer to the solution.

The puzzles

You can stay up-to-date on the puzzles by follow this blog or following me on twitter.

Puzzles published so far:

Clowns (solution) How can you fit 20 clowns into a Volkswagen?
Dreams (solution) Am I still dreaming?
Car (part 2, solution) Vroom!
Liquid (includes solution) Program your way past airport security
Ball (solution) Play ball!
Chicken (solution) Which was first, the chicken or the egg?
Cookies (solution) Learn how to count down with Count von Count and Cookie Monster
Square Root (solution) Find the square root of a huge number, without even looking at it
Tweet (solution) Exploring the power a superclass has over its subclass
No Dot (solution) Hello World without using a dot, backslash, or semicolon