jGuru
Register Email     Password Forgot your
password?
HOME FAQS FORUMS DOWNLOADS ARTICLES PEERSCOPE LEARN

  Search   jGuru Search Help

Question How can I implement conditional compilation in Java?
Derived from A question posed by Jan Sundin
Topics Java:Language, Java:Language:Reflection, Java:Language:Syntax, Java:Language:Semantics
Author John Zukowski PREMIUM
Created May 29, 2000 Modified May 29, 2000


Answer

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;
  }

and

  // Test.java
  public class Test {
    public static void main(String [] args) {
      if(Debug.printDebug)
        System.out.println("debugging enabled");
      else
        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.



Is this item helpful?  yes  no     Previous votes   Yes: 1  No: 0



Comments and alternative answers

Comment on this FAQ entry

How can I implement conditional compilation in Java?
Matt Doar, Nov 27, 2001
No-one else has mentioned it, so it may be worth pointing out that conditional compilation is also used to enable an application to compile and run using multiple JDK versions.

E.g. JDK1.4 introduces methods which do not exist in JDK1.3. If you call those new methods directly, your app will no longer compile with JDK1.3. Instead check the version using System.getProperty("java.version") and then use reflection to invoke the method only if the appropriate version is being used.

~Matt

Is this item helpful?  yes  no     Previous votes   Yes: 1  No: 0



Reply to this answer/comment  Help  



Ask A Question



 
Related Links

JavaLanguage FAQ

JavaLanguage Forum

Sun's Java Tutorial

What is the Java Platform

JavaWorld Magazine

IBM developerWorks

Java 2 SDK Tools and Utilities

The official Sun JavaDoc 1.1 documentation

The official Sun JavaDoc 1.2 documentation

The official Sun JavaDoc 1.3 documentation

The official Sun JavaDoc J2EE documentation

Wish List
Features
About jGuru
Contact Us

 



The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers