How can I implement conditional compilation in Java?

John Zukowski

In the general case, conditional compilation in Java is not needed where it would be needed in C/C++ because the same class files must run on all platforms. This is not always the case, as you may want either testing code included only in testing mode, or you may want certain features enabled (included in the .class files) if the user registers / buys a certain version. With that said, here are some different options available, from various community members:

According to Robert Baruch:

For operating-system dependencies, your code really should work for different operating systems by checking the os.name, os.arch, and os.version properties (see System.getProperties()).

According to Finley McWalter:

There's at least two ways to do this...

If you already have a C compiler, you can use the preprocessor part of it by itself, and feed it Java source rather than C. Depending on which compiler you have available, this might be called cpp, or might be a mode of the C compiler.

Here, for example, is how to do it using GNU gcc:

cp TestS.java TestS.c
gcc -E -P TestS.c > Test.java
rm -f TestS.c
javac Test.java

This example above is a bit complicated because gcc will only preprocess files with a .c extension (not all preprocessors are so picky). You also have to remember Java's restriction that public class Test must be defined in file Test.java - so in the example above that's the result of the preprocessor phase, and you actually edit a file with another name (e.g. TestS.java).

This approach gives you all of the features of the C preprocessor (#include, #ifdef, #define, __FILE__, __LINE__, #undef, ## etc.).

According to Terence Parr:

Check out Doug Tidwell's cool article on such a critter: http://www-4.ibm.com/software/developer/library/cprep/. It has a discussion of why Java has no preprocessor.

According to Rob Edmondson:

Mocha Source by MochaSoft has a preprocessor that supports conditional compiling. It also has a good source code obfuscator. It processes the Java code before compilation.

According to Finley McWalter, Terence Parr, Andre van Dalen, Mikael Jakobsson, Greg Boettcher, and Robert Baruch:

The other way you can get conditional compiler is to use a feature of the Java language that's not commonly known - Java does have conditional compilation, built right in.

This works because the compiler will not generate unreachable code - and it's smart enough to recognize and handle if() statements that will always have the same result. Here's a quick example. We have two classes:

  // Debug.java
  public class Debug {
    public static final boolean printDebug = true;


  // Test.java
  public class Test {
    public static void main(String [] args) {
        System.out.println("debugging enabled");
        System.out.println("debugging disabled");

Because Debug.printDebug is final, the compiler can know that the if statement will always be true, so it only generates the "debugging enabled" line - if you don't believe me, look at the class file with javap -c and you'll not see any bytecode for the if or for the else-clause, and the "...disabled" string isn't in the class file at all - it's been conditionally compiled away.

Now, if you change the value of printDebug to false, and recompile both source files, the else code will be present, and again there's no code in the classfile for the if.

According to Mikael Jakobsson:

The above is a solution, but I do not recommend it unless you have very, very specific needs. It is ususally better (at least from an OO viewpoint) to design your code in such a manner that you do not need to preprocess the code.

It is usually possible to separate the pieces of code that are optional into separate classes and then at runtime decide which implementation to use through Reflection.

The Abstract Factory design pattern may be one approach to the problem (See the book Design Patterns, by Erich Gamma et.al for detailed info). There are certainly other design solutions as well.

According to Brian O'Byrne:

If you need to do conditional compilation based on environment variables to such, your only option is to have a script which modifies your code before you pass it into the compiler. This script would set the INCLUDE_ constants based on outside criteria. Something written in awk or perl would do nicely.