Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2007

Tip: Looking for answers? Try searching our database.

compile directive for conditional compile for Java 1.4 versus Java 5

Thread view: 
timjowers - 02 Jul 2007 19:11 GMT
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();
        }
    }
}
Jeff Higgins - 02 Jul 2007 20:01 GMT
>   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>
Roedy Green - 02 Jul 2007 20:56 GMT
>        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
Arne Vajhøj - 03 Jul 2007 03:06 GMT
>>         long start = System.currentTimeMillis(); // only compile this line
>> in Java 1.4
[quoted text clipped - 7 lines]
>
> java.vm.version = 1.6.0_01-b06

That will not help at compilation.

Arne
timjowers - 03 Jul 2007 15:31 GMT
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
Arne Vajhøj - 04 Jul 2007 22:45 GMT
> 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
[quoted text clipped - 11 lines]
>         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
Arne Vajhøj - 03 Jul 2007 03:06 GMT
>    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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.