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