This path leads us to the 46 bytes solution that makes use of the following hack.Īnd if you compile it without the debugging info, you get a tiny class file.Īnd this was for a long time the smallest class which we also thought of as the reference solution for the challenge. So if we want to remove the reference, we need to be the. The constant_pool entry at that index must be a CONSTANT_Class_info structure representing the class Object." "For an interface, the value of the super_class item must always be a valid index into the constant_pool table. Note that the 4th chapter of the Java Virtual Machine specification, the one that specifies the class file format, says: Perhaps the only thing that we can shrink further is the superclass reference which is the java/lang/Object. Note that quite a large chunk of the class file now is the mandatory structure: the major/minor versions, modifier flags, etc.
The next stop, which many of you uncovered and many considered to be the lower limit is 46 bytes. MD5 checksum b33652e5fe31c274287cd991c85c9e8aĪnd thus, many of you stopped at 53 bytes and didn't go further into the darkness of javac hacks. If you look under the hood, you'll see that it looks indeed just like the package-info, but with a shorter name: Lo and behold, we have a very reasonable candidate for the smallest Java class file generated by javac. Well, and that gives us the next step of the solution, let’s try an empty interface, hoping that we can specify a shorter name, so the constant “X/package-info” would be like 1–2 characters long. We can see that quite a bit of space is taken by the constants “X/package-info” and “java/lang/Object.” If you didn’t know it beforehand, looking at the javap output should hint you that the package-info looks a lot like an interface (see the ACC_INTERFACE flag there?). MD5 checksum 2846963a79ebed75a07bb26bb3be5a55įlags: ACC_INTERFACE, ACC_ABSTRACT, ACC_SYNTHETIC If you’re curious, this is what the package-info.class looks like from the inside:Ĭlassfile /Users/shelajev/repo/tmp/java-small-class-challenge/package-info.class It tells javac not to output the debug symbols, making the resulting file much smaller. Note the crucial -g:none option we passed to the javac command.
$ javac -Xpkginfo:always -g:none package-info.java
#Compiling java class code
So can we maybe shrink the source file even further? An interesting attempt came from Tagir Valeev, from JetBrains, master of the static code inspections, creator of numerous Java puzzles himself, and author of StreamEx library that enhances Java 8 Streams functionality, and much more! Check this out, one can create an almost empty package-info file, that will be compiled into a package-info.class file, right? And since it’s almost empty it should be quite small. If you’re interested in exploring the resulting class file, you can see what is inside it by using the javap utility that comes with your JDK.Ĭlassfile /Users/shelajev/repo/tmp/java-small-class-challenge/A.class When we compile if with javac, it creates the A.class file, which we weight for the size:Īs we can see a straightforward way to get a small class file by compiling a small Java file gets us only into hundreds of bytes. Compile a simple, empty class file and check the size of the resulting class file. Spoiler alert ! If you want to see whether you can convince javac to generate a smaller class file, this is the right time to stop reading and open your terminal. In this post, I’d like to show you a couple of solutions that I got from our readers.