compile directive for conditional compile for Java 1.4 versus Java 5 - Java

This is a discussion on compile directive for conditional compile for Java 1.4 versus Java 5 - Java ; 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 ...

+ Reply to Thread
Results 1 to 7 of 7

compile directive for conditional compile for Java 1.4 versus Java 5

  1. Default compile directive for conditional compile for Java 1.4 versus Java 5


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


  2. Default Re: compile directive for conditional compile for Java 1.4 versus Java 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.
    >


    <http://www.google.com/search?q=java+conditional+compilation&hl=en&start=10&sa=N>



  3. Default Re: compile directive for conditional compile for Java 1.4 versus Java 5

    On Mon, 02 Jul 2007 18:11:21 -0000, timjowers <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

  4. Default 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

  5. Default 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 <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

  6. Default Re: compile directive for conditional compile for Java 1.4 versus Java 5

    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


  7. Default 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

+ Reply to Thread

Similar Threads

  1. How to compile a java source with UTF-8 encoding in cmd?
    By Application Development in forum Java
    Replies: 2
    Last Post: 08-22-2007, 09:29 PM
  2. Compile C++/Java into C--Possible ?
    By Application Development in forum Java
    Replies: 3
    Last Post: 03-06-2007, 11:55 AM
  3. don't know how to compile a java program w/ Java 1.0.5_2
    By Application Development in forum Java
    Replies: 1
    Last Post: 03-19-2005, 06:14 PM
  4. Compile java to bytecode & execute in java runtime?
    By Application Development in forum Java
    Replies: 0
    Last Post: 07-20-2004, 02:21 PM
  5. guide for compile java for vrml
    By Application Development in forum vrml
    Replies: 0
    Last Post: 12-04-2003, 06:27 AM