compile directive for conditional compile for Java 1.4 versus Java 5

Discussion in 'Java' started by timjowers, Jul 2, 2007.

  1. timjowers

    timjowers Guest

    Does a way exist to instruct the Java compiler to compile code only
    if a certain version of Java is supported? For example, I may want to
    measure with milliseconds in Java 1.4 or nanoseconds if I'm running in
    Java 5.

    import java.io.IOException;

    public class JVMLoadTimeRunner {

    public static void main(String[] args) {

    long start = System.currentTimeMillis(); // only compile this line
    in Java 1.4
    //long start = System.nanoTime(); // only compile this line in Java
    5

    try {
    Process newP = Runtime.getRuntime().exec( cmd );
    newP.waitFor();
    long end = System.currentTimeMillis();
    System.out.println( "Process Execution took " + (float)(end-start)/
    1000F + " seconds" );
    // Java 5
    // System.out.println( "Process Execution took " + (float)(end-
    start)/1000000000F + " seconds" );
    } catch (IOException e) {
    e.printStackTrace();
    } catch (InterruptedException e) {
    System.out.println( "Process wait threw an Exception" );
    e.printStackTrace();
    }
    }
    }
     
    timjowers, Jul 2, 2007
    #1
    1. Advertising

  2. timjowers

    Jeff Higgins Guest

    timjowers wrote:
    >
    > Does a way exist to instruct the Java compiler to compile code only
    > if a certain version of Java is supported? For example, I may want to
    > measure with milliseconds in Java 1.4 or nanoseconds if I'm running in
    > Java 5.
    >


    <http://www.google.com/search?q=java+conditional+compilation&hl=en&start=10&sa=N>
     
    Jeff Higgins, Jul 2, 2007
    #2
    1. Advertising

  3. timjowers

    Roedy Green Guest

    On Mon, 02 Jul 2007 18:11:21 -0000, timjowers <>
    wrote, quoted or indirectly quoted someone who said :

    > long start = System.currentTimeMillis(); // only compile this line
    >in Java 1.4
    > //long start = System.nanoTime(); // only compile this line in Java


    You can look at a system property at run time and use that in an if.

    java.specification.version = 1.6

    java.version = 1.6.0_01

    java.vm.version = 1.6.0_01-b06

    see http://mindprod.com/jgloss/properties.html
    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Jul 2, 2007
    #3
  4. Re: compile directive for conditional compile for Java 1.4 versusJava 5

    timjowers wrote:
    > Does a way exist to instruct the Java compiler to compile code only
    > if a certain version of Java is supported? For example, I may want to
    > measure with milliseconds in Java 1.4 or nanoseconds if I'm running in
    > Java 5.


    Not as you can in C/C++.

    You could use an external preprocessor.

    But I would drop the idea.

    Arne
     
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Jul 3, 2007
    #4
  5. Re: compile directive for conditional compile for Java 1.4 versusJava 5

    Roedy Green wrote:
    > On Mon, 02 Jul 2007 18:11:21 -0000, timjowers <>
    > wrote, quoted or indirectly quoted someone who said :
    >
    >> long start = System.currentTimeMillis(); // only compile this line
    >> in Java 1.4
    >> //long start = System.nanoTime(); // only compile this line in Java

    >
    > You can look at a system property at run time and use that in an if.
    >
    > java.specification.version = 1.6
    >
    > java.version = 1.6.0_01
    >
    > java.vm.version = 1.6.0_01-b06


    That will not help at compilation.

    Arne
     
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Jul 3, 2007
    #5
  6. timjowers

    timjowers Guest

    FWIW, I decided on the exploit of compiler optimization. The technique
    is to create unreachable code with as if(false) and put your Java 5
    code there. Make that block reachable to compile in Java 5. E.g.

    boolean JAVA_5_PLUS = false;
    // Java by design lacks conditional compilation so various
    workarounds
    // are to comment out code, use a custom preprocessor (see Munge
    from the Swing team),
    // or use if( false ) or such blocks (which are normally not
    compiled due to optimizing them out.)
    if( JAVA_5_PLUS )
    start = System.nanoTime();
    else
    start = System.currentTimeMillis();
    TimJowers
     
    timjowers, Jul 3, 2007
    #6
  7. Re: compile directive for conditional compile for Java 1.4 versusJava 5

    timjowers wrote:
    > FWIW, I decided on the exploit of compiler optimization. The technique
    > is to create unreachable code with as if(false) and put your Java 5
    > code there. Make that block reachable to compile in Java 5. E.g.
    >
    > boolean JAVA_5_PLUS = false;
    > // Java by design lacks conditional compilation so various
    > workarounds
    > // are to comment out code, use a custom preprocessor (see Munge
    > from the Swing team),
    > // or use if( false ) or such blocks (which are normally not
    > compiled due to optimizing them out.)
    > if( JAVA_5_PLUS )
    > start = System.nanoTime();
    > else
    > start = System.currentTimeMillis();


    I am very surprised if that compiles with the 1.4 compiler.

    Compile with 1.5 and run on 1.4 should work.

    Arne
     
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Jul 4, 2007
    #7
  8. timjowers

    heyjude

    Joined:
    Feb 2, 2011
    Messages:
    1
    check out the wwyt conditional compilation pre-processor

    There is an external tool called wwyt which does just that. It's a Windows command line tool, it pre-process your Java source files, which are embedded with conditional compile directives (like #if, #else, ...). You then let compiler compile these converted files and get the .class files you want. After compilation, you should run wwyt tool again so that the converted files are restored back to their original status.

    URL is this: adarian dot com slash wwyt

    Hope this helps.
     
    heyjude, Feb 2, 2011
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthew Louden
    Replies:
    1
    Views:
    6,945
    Scott M.
    Oct 11, 2003
  2. RC

    Conditional Directive

    RC, May 16, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    419
    Karl Seguin
    May 16, 2005
  3. Simon

    Conditional directive

    Simon, Nov 25, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    339
    intrader
    Nov 25, 2005
  4. Replies:
    1
    Views:
    451
    Cowboy \(Gregory A. Beamer\)
    Apr 11, 2008
  5. Paul Butcher
    Replies:
    12
    Views:
    729
    Gary Wright
    Nov 28, 2007
Loading...

Share This Page